std::ferror

From cppreference.com
< cpp‎ | io‎ | c
 
 
 
 
Defined in header <cstdio>
int ferror( std::FILE* stream );

Checks the given stream for errors.

Contents

[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
       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");
}


[edit] 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