Fgets Considerations when reading a file

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.