ferror

Defined in header <stdio.h>
int ferror( FILE *stream );

Checks the given stream for errors.

Parameters

stream - the file stream to check

Return value

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

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
       putchar(c);
    }
 
    if (ferror(fp))
        puts("I/O error when reading");
    else if (feof(fp))
        puts("End of file reached successfully");
 
    fclose(fp);
}

References

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

See also

clears errors
(function)
checks for the end-of-file
(function)
displays a character string corresponding of the current error to stderr
(function)
C++ documentation for ferror
doc_C_Language
2016-10-10 18:35:11
Comments
Leave a Comment

Please login to continue.