1 text file A.txt content as follows
2 C code
FILE *fil; if (! ( fil = fopen ("/home/rudy/projects/paser/a.txt", "RB")) {printf ("File I/O error,the file is not exist\n"); Exit (0); } int line = 0; while (!feof (fil)) {if (fgetc (fil) = = ' \ n ') {++line; }} ++line; Fclose (fil); if (! ( fil = fopen ("/home/rudy/projects/paser/a.txt", "RB")) {printf ("File I/O error,the file is not exist\n"); Exit (0); } char * * all = (char *) malloc (line * sizeof (char *)); for (int icurrlineindex = 0, icurrlineindex < line; ++icurrlineindex) {All[icurrlineindex] = (char *) malloc (20 ); Fgets (All[icurrlineindex], +, fil); } for (int icurrlineindex = 0; icurrlineindex < line; ++icurrlineindex) {printf ("This is len =%d \ n", Strlen (AL L[icurrlineindex])); for (int i = 0;i <11;i++) {if (all[icurrlineindex][i] = = ' + ') {printf ("%s\n", "t He is 0 "); }else if (all[icurrlineindex][i]== ' \ n '{printf ("%s\n", "This is N"); }else{printf ("%c", All[icurrlineindex][i]); }} printf ("\ n"); }
Output results
This was len = 1 2 3 4 5 6 7 8 9 This was n this is 0 this is len = ten a b c D e F g h I This is n this is 0 this is Len = 9 1 2 3 4 5 6 7 8 9 This was 0 this is 0
Summary: fgets read, if the specified read size is less than the actual row size then do not add \ n to do the end, use the \ r, and then read the current row data that is not read as a new line start
Fgets read, if the read size is specified. is greater than the actual row size, then \ n is added to the end. Add
It does not count as an element in the effective length, \ n The effective length of the element.
Fgets Considerations when reading a file