std::ferror

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

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_CPP
2016-10-11 10:02:45
Comments
Leave a Comment

Please login to continue.