ferror

This is preliminary documentation and subject to change.

ferror

Tests for an error on a stream.

Function Required Header
ferror <stdio.h>
int ferror( FILE *stream );

Libraries

All versions of the C run-time libraries.

Parameters

  • stream
    Pointer to FILE structure

Return Values

If no error has occurred on stream, ferror returns 0. Otherwise, it returns a nonzero value.

Remarks

The ferror routine (implemented both as a function and as a macro) 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.

Example

/* FEOF.C: This program uses feof to indicate when
 * it reaches the end of the file FEOF.C. It also
 * checks for errors with ferror.
 */

#include <stdio.h>
#include <stdlib.h>

void main( void )
{
   int  count, total = 0;
   char buffer[100];
   FILE *stream;

   if( (stream = fopen( "feof.c", "r" )) == NULL )
      exit( 1 );

   /* 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 ) )      {
         printf( "Read error" );
         break;
      }

      /* Total up actual bytes read */
      total += count;
   }
   printf( "Number of bytes read = %d\n", total );
   fclose( stream );
}

Output

Number of bytes read = 745
 

See Also

clearerr, feof, fopen