Check the error status of a stream.
#include <stdio.h> int ferror (FILE *stream);
stream
A pointer to a file stream.
The ferror() function returns the error status of the last read or write operation on stream. The function does not reset its error status.
This facility may have limited capability on configurations of the EWL that run on platforms that do not have console input/output or a file system.
ferror() returns a nonzero value if stream's error status is on, and returns zero if stream's error status is off.
#include <stdio.h> #include <stdlib.h> int main(void) { FILE *f; char filename[80], buf[80]; int ln = 0; // get a filename from the user printf("Enter a filename to read.\n"); gets(filename); // open the file for input if (( f = fopen(filename, "r")) == NULL) { printf("Can't open %s.\n", filename); exit(1); } // read the file one line at a time until end-of-file do { fgets(buf, 80, f); printf("Status for line %d: %d.\n", ln++, ferror(f)); } while (feof(f) == 0); // close the file fclose(f); return 0; } Output: Enter a filename to read. itwerks Status for line 0: 0. Status for line 1: 0. Status for line 2: 0.