Namespaces
Variants
Views
Actions

std::ferror

From cppreference.com
< cpp‎ | io‎ | c
Revision as of 22:00, 31 May 2013 by P12bot (Talk | contribs)

 
 
 
 
Defined in header <cstdio>
int ferror( FILE *stream );

Checks the given stream for errors.

Contents

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


See also

clears errors
(function) [edit]
checks for the end-of-file
(function) [edit]
displays a character string corresponding of the current error to stderr
(function) [edit]
C documentation for ferror