The ferror routine tests for a reading or writing error on the file associated with stream. If an error has occurred, the error indicator for the stream remains set until the stream is closed or rewound, or until clearerr is called against it.
int ferror( FILE * stream )
If no error has occurred on stream, ferror returns 0. Otherwise, it returns a nonzero value.
//The following example uses feof to indicate when it reaches the end of your current C file. It also checks for errors with ferror.
int count, total = 0;
// open your current using C file for read
stream = fopen( __FILE__, "r" );
if(stream == NULL )
printf( "The current C file was not opened\n" );
// Cycle until end of file reached:
while( !feof( stream ) )
// Attempt to read in 10 bytes:
count = fread( buffer, sizeof( char ), 100, stream );
if( ferror( stream ) )
// Total up actual bytes read
total += count;
printf( "Number of bytes read = %d\n", total );
fclose( stream );