Why is “while ( !feof (file) )” always wrong?
-
12-11-2019 - |
Question
I've seen people trying to read files like this in a lot of posts lately.
Code
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
char *path = argc > 1 ? argv[1] : "input.txt";
FILE *fp = fopen(path, "r");
if( fp == NULL ) {
perror(path);
return EXIT_FAILURE;
}
while( !feof(fp) ) { /* THIS IS WRONG */
/* Read and process data from file… */
}
if( fclose(fp) == 0 ) {
return EXIT_SUCCESS;
} else {
perror(path);
return EXIT_FAILURE;
}
}
What is wrong with this loop?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow