Home > Back-end >  Help feof of C language
Help feof of C language

Time:11-14

Why the last line of the output twice? Thank you very much!

CodePudding user response:

To evaluate the fgets return value, when they read the EOF returns NULL, then it is good to break out of the loop ~

CodePudding user response:

Judge the return value first, then print, while judging feof again

CodePudding user response:

Change, so look at the line not line?

 # include & lt; stdio.h> 

Int main (void)
{
FILE *fp;
Char buf [BUFSIZ];

If ((fp=fopen (__FILE__, "r"))==NULL)
{
Perror (" fopen ");
return 1;
}

while(! The feof (fp) & amp; & The fgets (buf, BUFSIZ, fp))
{
Printf (" % s ", buf);
}

return 0;
}
  • Related