salut tout le monde
je veux compter le nombre des lignes dans un fichier csv ensuite je veux calculer le nombre des champs par ligne
j'ai fait une fonction qui compte le nbre des lignes:
ensuite j'ai fait une fonction qui va calculer le nombre des champs dans une ligne,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
39
40 static int line_count ( char *filename) { printf("beginning of line_count() for the file: %s \n",filename); trace("beginning of line_count() \n"); int nb_l = 0; int nb_lines ; char c={0}; FILE* file_in; /* stream associated to filename */ file_in = fopen (filename, "r"); if (file_in == NULL) { printf("ERROR: Impossible to open the file %s \n", filename); printf("ERROR CODE IS %d \n", errno); exit (-1); return(0); } /* nb_lines contains the number of lines in the file */ while ((c=getc(file_in)) != EOF) { if ((c=getc(file_in)) =='\n') { nb_l++; printf("the counter reached the value : %s \n",nb_l); } } print("we finishe the count of lines, we will close the file"); fclose (file_in); nb_lines = nb_l+1; printf("number of lines is %d \n", nb_lines); printf("end of the function line_count() \n \n"); return (nb_lines);
cette fonction fait appel à la fonction qui compte le nombre des lignes.
je ne sais pas pourquoi dans cette 2éme fonction la boucle while boucle à l'infini
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 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}; printf("we will count the number of lines first in the file : %s \n", filename); nb_lignes = line_count(filename); if (nb_lignes == 0) { printf("the file %s did not contain any line \n",filename); return(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=getc(file_in)) != EOF) { printf("while we did not reached the EOF \n"); while ((c=getc(file_in)) != '\n') { if ((c=getc(file_in)) ==',') { printf("we increment the counter when we meet a comma \n"); 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+1; printf("the number of fields is: %s \n",nb_fields); printf("end of field_count() \n \n "); return (nb_fields); }
en plus la première fonction line_count m'affiche toujours 1 malgé que j'ai fait passé un fichier qui contient trois lignes
Partager