ferror() — Test for Read/Write Errors

Format

#include <stdio.h>
int ferror(FILE *stream);

Language Level

ANSI

Threadsafe

Yes

Description

The ferror() function tests for an error in reading from or writing to the given stream. If an error occurs, the error indicator for the stream remains set until you close stream, call the rewind() function, or call the clearerr() function.

Return Value

The ferror() function returns a nonzero value to indicate an error on the given stream. A return value of 0 means that no error has occurred.

Example

This example puts data out to a stream, and then checks that a write error has not occurred.
#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   char *string = "Important information";
   stream = fopen("mylib/myfile","w");
 
   fprintf(stream, "%s\n", string);
   if (ferror(stream))
   {
      printf("write error\n");
      clearerr(stream);
   }
   if (fclose(stream))
      perror("fclose error");
}

Related Information