std::ferror - cppreference.com (original) (raw)
| I/O manipulators |
|---|
| Print functions (C++23) |
| C-style I/O |
| Buffers |
| basic_streambuf |
| basic_filebuf |
| basic_stringbuf |
| basic_spanbuf(C++23) |
| strstreambuf(C++98/26*) |
| basic_syncbuf(C++20) |
| Streams |
| Abstractions |
| ios_base |
| basic_ios |
| basic_istream |
| basic_ostream |
| basic_iostream |
| File I/O |
| basic_ifstream |
| basic_ofstream |
| basic_fstream |
| String I/O |
| basic_istringstream |
| basic_ostringstream |
| basic_stringstream |
| Array I/O |
| basic_ispanstream(C++23) |
| basic_ospanstream(C++23) |
| basic_spanstream(C++23) |
| istrstream(C++98/26*) |
| ostrstream(C++98/26*) |
| strstream(C++98/26*) |
| Synchronized Output |
| basic_osyncstream(C++20) |
| Types |
| streamoff |
| streamsize |
| fpos |
| Error category interface |
| iostream_category(C++11) |
| io_errc(C++11) |
| Types and objects | |
|---|---|
| FILE fpos_t | stdinstdoutstderr |
| Functions | |
| File access fopen freopen fclose fflush fwide setbuf setvbuf Direct input/output freadfwrite Unformatted input/output fgetcgetc fgets fputcputc fputs getchar gets(until C++14) putchar puts ungetc fgetwcgetwc fgetws fputwcputwc fputws getwchar putwchar ungetwc Formatted input scanffscanfsscanf vscanfvfscanfvsscanf(C++11)(C++11)(C++11) wscanffwscanfswscanf vwscanfvfwscanfvswscanf(C++11)(C++11)(C++11) | Formatted output printffprintfsprintfsnprintf(C++11) vprintfvfprintfvsprintfvsnprintf(C++11) wprintffwprintfswprintf vwprintfvfwprintfvswprintf File positioning ftell fgetpos fseek fsetpos rewind Error handling clearerr feof ferror perror Operations on files remove rename tmpfile tmpnam |
| Defined in header | | | | -------------------------------------------------------------------------- | | | | 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
Run this code
#include #include #include #include int main() { const char fname = std::tmpnam(nullptr); std::FILE f = std::fopen(fname, "wb"); std::fputs("\xff\xff\n", f); // not a valid UTF-8 character sequence std::fclose(f); std::setlocale(LC_ALL, "en_US.utf8"); f = std::fopen(fname, "rb"); std::wint_t ch; while ((ch=std::fgetwc(f)) != WEOF) // attempt to read as UTF-8 std::printf("%#x ", ch); if (std::feof(f)) puts("EOF indicator set"); if (std::ferror(f)) puts("Error indicator set"); }
Output:
Error indicator set
[edit] See also
| clearerr | clears errors (function) [edit] |
|---|---|
| feof | checks for the end-of-file (function) [edit] |
| fail | checks if an error has occurred (public member function of std::basic_ios<CharT,Traits>) [edit] |
| C documentation for ferror |