fichiers csv et langage C: compter le nombre des champs
j'ai écrit une fonction qui doit compter le nombre des champs dans une ligne d'un fichier .csv
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| static int field_count ( char* filename)
{
printf("Beginning of field_count() for the file : %s \n", filename);
int nb_f = 0;
int nb_fields ;
//int nb_lignes;
FILE* file_in;
char c={0};
file_in = fopen(filename, "r");
if (file_in == NULL)
{
printf("ERROR: Impossible to open the temporary file %s in reading mode \n", filename );
printf("ERROR CODE IS %d \n", errno);
exit (-1);
return(0);
}
/* nb_fields contain the number of fields in a line */
while ((c!= EOF) && (c !='\n'))
{
c = getc(file_in);
if (c ==',')
{
nb_f++;
printf("the counter reached the value :%d \n",nb_f);
}
}
printf("we finished the count, we will close the file");
fclose (file_in);
nb_fields = nb_f;
printf("the number of fields is: %s \n",nb_fields);
printf("end of field_count() \n \n ");
return (nb_fields);
} |
lors de l'exécution, elle m'affiche le message "the counter reached the value" 9 fois ensuite elle m'affiche Segmentation Fault
le fichier sur lequel j'ai testé est:
125895,A,8807,USER,TRD,1,PHO,314-955-5793 (CMO), ,
125895,D,8807,USER,TRD,1,SWF, , ,
125895,A,8807,USER,TRD,1,TLX, , ,
pourquoi le segmentation fault????
please i need help :)