C 文件未被读取

C 文件未被读取,c,fopen,C,Fopen,免责声明:我代码中的一些单词是法语的 当我编译代码时,没有bug,但当我运行它时,唯一出现的是一句“Contenu du tableau avant le tri”。我想我需要读取的文件(notes.txt)没有被读取。有人知道问题出在哪里吗?这是我的密码: #include <stdio.h> #include <ctype.h> #include <string.h> #define MAX_PERS 200 typedef struct {

免责声明:我代码中的一些单词是法语的

当我编译代码时,没有bug,但当我运行它时,唯一出现的是一句“Contenu du tableau avant le tri”。我想我需要读取的文件(notes.txt)没有被读取。有人知道问题出在哪里吗?这是我的密码:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX_PERS 200



typedef struct
{
    int numero;
    float note;
    char nom, prenom;
}   donnees;

donnees tab[MAX_PERS];
int nbPers=0;



void lire(donnees tab[], int *n)
{
    FILE *aLire = fopen("notes.txt", "r");

    int nb=0;

    while(!feof(aLire))
    {
        fscanf(aLire, "%d%s%s%f\n", &tab[nb].numero, &tab[nb].nom, &tab[nb].prenom, &tab[nb].note);
        nb++;  
    }   
    fclose(aLire);
    *n = nb;   
}



void afficher(donnees tab[], int nb, char *quand)
{
    int i;

    printf("Contenu du tableau %s le tri\n\n", quand);

    for(i=0; i<nb; i++)
        printf( "%4d %15s %15s %2.2f\n", tab[i].numero, tab[i].nom, tab[i].prenom, tab[i].note);
}



void echanger (donnees *P1, donnees *P2)
{ 
    donnees tempo;

    tempo = *P1 ;
    *P1 = *P2;
    *P2 = tempo;
}



void  partitionner ( donnees tab[], int debut, int fin, int *P )
{ 
    int G = debut , D = fin  ;

    float Val_Pivot = tab[debut].note;

    while ( G <= D  &&  tab[G].note <= Val_Pivot) G++;

    while ( tab[D].note > Val_Pivot) D--;

        if ( G < D ) echanger(&tab[G], &tab[D]);

    while ( G <= D ) ;

        *P = D ;

        echanger (&tab[debut], &tab[D]);
}



void quickSort ( donnees tab[], int gauche, int droite )
{ 
    int indPivot ;

    if (droite > gauche)
        {
            partitionner ( tab, gauche, droite, &indPivot);
            quickSort ( tab, gauche, indPivot - 1 );
            quickSort ( tab, indPivot + 1, droite);
        }
}




int main()
{
    donnees tab[MAX_PERS];

    lire(tab, &nbPers);

    afficher(tab, nbPers, "avant");



    return 0;
}
#包括
#包括
#包括
#定义最大值为200
类型定义结构
{
整数;
浮动票据;
char-nom,prenom;
}唐尼;
donnees选项卡[最大值];
int-nbPers=0;
无效lire(donnees选项卡[],内部*n)
{
文件*aLire=fopen(“notes.txt”、“r”);
int nb=0;
而(!feof(aLire))
{
fscanf(aLire,“%d%s%s%f\n”,&tab[nb].numero,&tab[nb].nom,&tab[nb].prenom,&tab[nb].note);
nb++;
}   
fclose(aLire);
*n=nb;
}
无效附加(donnees选项卡[],内部编号,字符*数量)
{
int i;
printf(“画面内容%s le tri\n\n”,quand);

对于(i=0;i验证
fopen
返回结果,因为如果由于某种原因打开失败,它将返回
NULL

不要依赖
!feof
来检测文件的结尾,因为您的文件可能会部分结束,请使用
fscanf
返回值(解析/读取的条目数)

然后在您的结构中,您使用
char
(一个字符)作为名称,其中您应该使用
char[一些长度]
char*

int numero;
float  note;
char[MAX_LENGTH] nom;
char[MAX_LENGTH] prenom;
// assuming that field are separated by space
while(fscanf(
"%d %"MAX_LENGTH"s %"MAX_LENGTH"s %f",
&numero,
nom,
prenom,
&note) == 4) {
   // copy data in new row in array
}

请提供文件内容的示例。对于初学者,请检查
fopen
fscanf
的返回值。此外,当程序无法按预期工作时,您应该做的是通过在调试器中运行或添加debug print语句对其进行调试。您做过任何操作吗?如果是,您发现了什么开始出错?如果不确定文件是否实际打开,请在
fopen
之后尝试
If(!aLire){/*error*/}
以检查
aLire
是否指向地址而不是NULL。您可以在函数lire()的末尾检查nb的值,一个简单的printf,用于检查它是否按预期工作。然后在afficher()内再次检查它。Bon courage。欢迎使用堆栈溢出!在
partitionner
函数中,似乎不太可能
while(非常感谢!我将
char-nom
char-prenom
更改为
char-nom[MAX\u-LENGTH]
char prenom[MAX_LENGTH]
现在终于可以工作了。已经被卡住两天了,谢谢!