Perché "why (! Feof (file))" è sempre sbagliato?
-
12-11-2019 - |
Domanda
Ho visto persone che cercano di leggere file come questo in molti post ultimamente.
Codice
#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;
}
}
Cosa c'è di sbagliato in questo ciclo?
Nessuna soluzione corretta
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow