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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, bad() returns true when the stream has set its badbit. This flag means something went seriously wrong, such as a damaged device or a lost connection to the buffer.

Do not mix it up with fail(). A simple typing mistake, like letters where a number is expected, only sets failbit. bad() stays false for such ordinary problems. 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 bad on any stream. It takes no arguments.

C++
bool broken = file.bad();

if (std::cin.bad()) {
    std::cerr << "stream is damaged\n";
}
CallWhat it does
file.bad()Returns true only when badbit is set.
s.setstate(std::ios::badbit)Sets the flag by hand. This is mostly useful for testing.
s.clear()Clears all flags, including badbit.

Most beginner programs never see badbit. When a read fails, look at fail() and eof() first.

Library declarations
C++
bool bad() const;

Parameters

ParameterDescription
Nonebad() takes no parameters.

Return Value

Return ValueDescription
truebadbit is set. The stream lost its integrity.
falsebadbit is not set. Other flags such as failbit may still be set.

C++ Examples

Example 1 — bad() After a Typing Mistake

Read a number from letters and compare bad() with fail().

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

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

    in >> n;

    std::cout << "bad:  " << (in.bad() ? "yes" : "no") << '\n';
    std::cout << "fail: " << (in.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. The text hello cannot become an int, so the read fails.
  2. A failed conversion sets only failbit. The stream itself is not damaged, so bad() says no.
  3. fail() says yes because it reports failbit. This is the common case you will meet in real programs.

Example 2 — bad() When a File Does Not Exist

Open a file that is not there. Opening fails, but the stream is not bad.

C++
#include <fstream>
#include <iostream>

int main() {
    std::ifstream file("no_such_file.txt");

    std::cout << "is_open: " << (file.is_open() ? "yes" : "no") << '\n';
    std::cout << "fail:    " << (file.fail() ? "yes" : "no") << '\n';
    std::cout << "bad:     " << (file.bad() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. std::ifstream tries to open no_such_file.txt. The name is made up, so the file is not found.
  2. A failed open sets failbit, so fail() returns true and is_open() returns false.
  3. badbit stays clear. The stream is not damaged, it just has nothing to read, so bad() is no.

Example 3 — Set and Clear badbit by Hand

Use setstate() to simulate a serious error and clear() to remove it.

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

int main() {
    std::stringstream s;

    std::cout << "start: " << (s.bad() ? "bad" : "not bad") << '\n';

    s.setstate(std::ios::badbit);
    std::cout << "after setstate: " << (s.bad() ? "bad" : "not bad") << '\n';
    std::cout << "good(): " << (s.good() ? "yes" : "no") << '\n';

    s.clear();
    std::cout << "after clear: " << (s.bad() ? "bad" : "not bad") << '\n';
    return 0;
}

How It Works

  1. A new std::stringstream starts with all flags clear, so bad() is false.
  2. setstate(std::ios::badbit) raises the flag. Now bad() is true and good() is false.
  3. clear() with no argument sets the state back to goodbit, which removes badbit again.

Frequently Asked Questions

bad() returns true when badbit is set. It means the stream had a serious error, such as a hardware problem or a broken buffer, and the stream may have lost data.
fail() is true for ordinary problems like wrong input or a file that cannot be opened (failbit), and also when badbit is set. bad() is true only for badbit, the serious case.
No. Typing letters when a number is expected sets only failbit. badbit stays clear.
You can call clear() to reset the flags, but the underlying problem may still exist. For example, a disk error will probably happen again.
Usually not. Check the result of the read itself, for example if (cin >> x), or use fail() and eof(). Check bad() when you handle files and need to tell a real I/O error from a normal end of file.

Next: clear()

Reset the stream error flags so it can be used again.

clear() 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.

7 people found this page helpful