C++ std::ios::rdstate() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, rdstate() gives you the whole error state of a stream in one value. The value is a bit mask: each of the flags eofbit, failbit, and badbit is one bit, and goodbit means no bit is set.

The functions good(), eof(), fail(), and bad() are shortcuts that look at parts of this same value. Use rdstate() when you want to see all the flags together. Include <iostream> to use it.

This is a member function of std::ios. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call rdstate on a stream. Use the & operator to test one flag.

C++
std::ios::iostate st = in.rdstate();

if (st & std::ios::failbit) {
    // failbit is set
}
CallWhat it does
in.rdstate()Returns the current state as an iostate value.
in.rdstate() == std::ios::goodbitTrue when no flag is set at all.
in.rdstate() & std::ios::eofbitNon-zero when eofbit is set. Any other flags are ignored.

Do not print the raw number. Its value is different on different compilers. Test the flags by name instead.

Library declarations
C++
iostate rdstate() const;

Parameters

ParameterDescription
Nonerdstate() takes no parameters.

Return Value

Return ValueDescription
iostateA bit mask of the flags that are set. std::ios::goodbit (zero) means that none is set.

C++ Examples

Example 1 — rdstate() on a Healthy Stream

A new string stream has no flags set, so its state equals goodbit.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("12");

    if (in.rdstate() == std::ios::goodbit) {
        std::cout << "state: goodbit (no flags)\n";
    }

    int n = 0;
    in >> n;

    if (in.rdstate() != std::ios::goodbit) {
        std::cout << "state changed after reading " << n << '\n';
    }
    return 0;
}

How It Works

  1. rdstate() on a new stream returns goodbit, so the first message is printed.
  2. in >> n reads 12 and hits the end of the text, which sets eofbit.
  3. Now rdstate() is not goodbit any more, so the second message appears even though the read worked.

Example 2 — Test Each Flag with &

Write a small helper that prints which flags are set, then use it after a failed read.

C++
#include <iostream>
#include <sstream>

void show(const std::ios& s) {
    std::cout << "eof:" << ((s.rdstate() & std::ios::eofbit) != 0)
              << " fail:" << ((s.rdstate() & std::ios::failbit) != 0)
              << " bad:" << ((s.rdstate() & std::ios::badbit) != 0) << '\n';
}

int main() {
    std::istringstream in("abc");
    int n = 0;

    show(in);
    in >> n;
    show(in);
    return 0;
}

How It Works

  1. show() takes the stream by reference and checks each flag with &. Each test gives 1 (set) or 0 (clear).
  2. The first call prints three zeros, because a new stream has no flags.
  3. Reading an int from abc fails, so the second call shows only fail:1. The stream has not reached the end and is not damaged.

Example 3 — Both eofbit and failbit

Read two numbers from text that has only one. The second read sets two flags.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("5");
    int a = 0;
    int b = 0;

    in >> a;
    in >> b;

    std::ios::iostate st = in.rdstate();
    std::cout << "eofbit set:  " << ((st & std::ios::eofbit) ? "yes" : "no") << '\n';
    std::cout << "failbit set: " << ((st & std::ios::failbit) ? "yes" : "no") << '\n';
    std::cout << "badbit set:  " << ((st & std::ios::badbit) ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. The first read gets 5 and sets eofbit because the text ends right after it.
  2. The second read has nothing to read, so it also sets failbit. Both flags can be set at the same time.
  3. The result of rdstate() is stored in a variable of type std::ios::iostate, and each flag is tested by name.

Frequently Asked Questions

It returns the stream state as an iostate bit mask. The bits are eofbit, failbit, and badbit. A value of goodbit means that no bit is set.
Use the & operator: if (s.rdstate() & std::ios::failbit). The result is non-zero when that flag is set.
good() returns a single true or false. rdstate() returns all the flags, so you can see which one is set.
The numeric values of the flags differ between compilers. Printing or comparing a raw number makes your program less portable. Test the flags by name.
Use clear(state) to replace the whole state, or setstate(flag) to add a flag. rdstate() only reads the state.

Next: read()

Read a fixed number of characters into a buffer.

read() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful