C++ Reference ยท Strings and I/O
C++ stream states
Explains C++ stream state flags: goodbit, eofbit, failbit and badbit, the good/fail/eof/bad functions, clear and setstate, input loops and exceptions.
Every iostream carries a small set of state flags that records whether its last operation worked. There are three: eofbit (the end of the input was reached), failbit (an operation failed, usually because the input did not have the expected format) and badbit (the stream is damaged: a low-level I/O error). With none set, the state is goodbit.
The key rule is that a stream in a failed state ignores further operations until you clear it. A single bad read leaves the variable unchanged (or set to zero) and every following read silently does nothing, so a loop that does not check the stream can run forever.
int x; if (std::cin >> x) { // success: the stream converts to true } else { // failbit (or badbit) is set }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
The flags and functions
| Flag | Set when | Test with |
|---|---|---|
goodbit |
no flag is set | good() |
eofbit |
an input operation reached the end of the data | eof() |
failbit |
the operation could not produce a value: wrong format, or nothing could be extracted; also set with badbit |
fail() |
badbit |
unrecoverable stream problem (the buffer failed, a disk error) | bad() |
| Function | Effect |
|---|---|
good() |
true only if none of the three flags is set. |
eof() |
true if eofbit is set. |
fail() |
true if failbit or badbit is set. |
bad() |
true if badbit is set. |
operator bool (explicit) |
!fail(). Use it in if (stream) and while (stream >> x). |
operator! |
fail(). |
rdstate() |
The current flags as a bit mask. |
clear(state = goodbit) |
Replaces the flags (default: clears everything). |
setstate(flags) |
Adds flags to the current state. |
exceptions(mask) |
Throws std::ios_base::failure when a flag in the mask becomes set. |
Usage notes
- Read in the loop condition.
while (in >> x)andwhile (std::getline(in, line))are correct.while (!in.eof())andwhile (in.good())run once too often whenever the input ends with whitespace or a newline (as most files do), because the flag is only set by the read that fails. eofdoes not mean error. A successful read of the last token on a line without a trailing newline may seteofbitand still succeed. Onlyfail()says the last read produced nothing.failbitblocks reads. After a bad number, callclear()and then remove the offending text withignore(max, '\n')or by reading it as a string, as in the second example. Clearing without removing it leads to an infinite loop.ignore,peekandgetalso fail oncefailbitis set. Clear first.seekgafter EOF. Since C++11seekgclearseofbitbefore moving but notfailbit, so callclear()yourself when rewinding.- Which bit for which error. A format error sets only
failbit. Reaching the end during a formatted read setseofbitandfailbit(if no value was extracted). An unrecoverable error setsbadbit, usually together withfailbit. - Exceptions are opt-in.
stream.exceptions(std::ios::badbit)is a popular middle ground: throw for real I/O errors and use flags for parse errors. Note thateofbitin the mask makes every normal end of input throw. - File streams set
failbitwhen opening fails. Testif (!file)right after opening. See ifstream and ofstream. - Output streams use the same flags.
badbitandfailbitafter<<mean the data was not written, for example a full disk.