Translated from: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong
I've seen a lot of people trying to read files this way 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 ) ) /* --ESTO ESTÁ MAL-- */
{
/* Lee y procesa datos desde el archivo… */
}
if( fclose ( fp ) == 0 )
{
return EXIT_SUCCESS;
}
else
{
perror(path);
return EXIT_FAILURE;
}
}
What is wrong with the loop while( !feof ( fp ) )
?