Skip to content
Tutorials Center
My learning

Topics

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.

C++
int x;
if (std::cin >> x) {
    // success: the stream converts to true
} else {
    // failbit (or badbit) is set
}

Try it

C++ Flags after different operations

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.

C++ Correct loops and recovery from bad input

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) and while (std::getline(in, line)) are correct. while (!in.eof()) and while (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.
  • eof does not mean error. A successful read of the last token on a line without a trailing newline may set eofbit and still succeed. Only fail() says the last read produced nothing.
  • failbit blocks reads. After a bad number, call clear() and then remove the offending text with ignore(max, '\n') or by reading it as a string, as in the second example. Clearing without removing it leads to an infinite loop.
  • ignore, peek and get also fail once failbit is set. Clear first.
  • seekg after EOF. Since C++11 seekg clears eofbit before moving but not failbit, so call clear() yourself when rewinding.
  • Which bit for which error. A format error sets only failbit. Reaching the end during a formatted read sets eofbit and failbit (if no value was extracted). An unrecoverable error sets badbit, usually together with failbit.
  • 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 that eofbit in the mask makes every normal end of input throw.
  • File streams set failbit when opening fails. Test if (!file) right after opening. See ifstream and ofstream.
  • Output streams use the same flags. badbit and failbit after << mean the data was not written, for example a full disk.