< cpp‎ | io‎ | c
Defined in header <cstdio>
int ferror( std::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 <cstdio>
#include <cstdlib>
int main()
    FILE* fp = std::fopen("test.txt", "r");
    if(!fp) {
        std::perror("File opening failed");
        return EXIT_FAILURE;
    int c; // note: int, not char, required to handle EOF
    while ((c = std::fgetc(fp)) != EOF) { // standard C I/O file reading loop
    if (std::ferror(fp))
        std::puts("I/O error when reading");
    else if (std::feof(fp))
        std::puts("End of file reached successfully");

[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