This is the original code:
#include <stdio.h>
int main ()
{
FILE * FP;
int ch;
fp = fopen ("D:\\aaaaa\\1.txt", "R");
while (!feof (FP))
{
ch = getc (FP);
Putchar (CH);
}
Fclose (FP);
return 0;
}
When you output it, you'll see that the output has a "space" in the end.
This is the improved code:
#include <stdio.h>
int main ()
{
FILE * FP;
int ch;
fp = fopen ("D:\\aaaaa\\1.txt", "R"); http://i.cnblogs.com/EditPosts.aspx?opt=1
while (ch = getc (FP),!feof (FP))
{
Putchar (CH);
}
Fclose (FP);
return 0;
}
Will find that the improved code will output the contents of the file normally, and will not add anything extra at the end.
Data found that the C language feof () function returned the last "read operation content." In the not-improved code, the while loop determines what fgetc () obtained in the last loop, which explains why the non-improved code will output "content" at the end.
Use Feof () to determine why the content will be more output at the end of the file