From cppreference.com
< c‎ | io
File input/output

File access
Direct input/output
Unformatted input/output
Formatted input
Formatted output
File positioning
Error handling
Operations on files
Defined in header <stdio.h>
int ferror( FILE *stream );

Checks the given stream for errors.


[edit] Parameters

stream - the file stream to check

[edit] Return value

Nonzero value if the file stream has errors occurred, 0 otherwise

[edit] Example

#include <stdio.h>
#include <stdlib.h>
int main(void)
    FILE* fp = fopen("test.txt", "r");
    if(!fp) {
        perror("File opening failed");
        return EXIT_FAILURE;
    int c; // note: int, not char, required to handle EOF
    while ((c = fgetc(fp)) != EOF) { // standard C I/O file reading loop
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");

[edit] References

  • C11 standard (ISO/IEC 9899:2011):
  • The ferror function (p: 339)
  • C99 standard (ISO/IEC 9899:1999):
  • The ferror function (p: 305)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • The ferror function

[edit] See also

clears errors
checks for the end-of-file
displays a character string corresponding of the current error to stderr
C++ documentation for ferror