##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::ferror

From cppreference.com
< cpp‎ | io‎ | c

Defined in header `<cstdio>` |  |
int ferror( 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

Run this code

    #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

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

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/io/c/ferror&oldid=57762"