Home

badbit

In the C++ standard library, badbit is a flag representing an irrecoverable error on a stream. It is part of the iostreams state set defined by std::ios_base::iostate and is used alongside eofbit and failbit to describe a stream's condition. The presence of badbit indicates that the stream has become unusable for further I/O operations, usually due to a hardware or serious library-level problem.

Badbit differs from eofbit (end-of-file) and failbit (a logical error in a stream operation) in that it

Detection and handling: You can query with stream.bad() to test for a badbit condition, or use rdstate()

Causes: Badbit is set by the underlying stream buffer when an irrecoverable error occurs on the input

Notes: Badbit is defined by the C++ standard library's ios_base::badbit and is one of the four iostate

signals
a
more
severe
condition
that
often
cannot
be
recovered
from
by
simple
retry.
In
practice,
many
standard
library
implementations
set
both
badbit
and
failbit
in
irrecoverable
situations,
and
a
stream
in
this
state
is
generally
not
reliable.
to
inspect
the
full
iostate
value,
and
retrieve
a
boolean
with
bool(stream).
You
can
clear
the
flags
with
stream.clear(),
typically
passing
std::ios_base::goodbit
to
reset
the
state,
though
this
does
not
repair
the
underlying
error.
If
exceptions
are
enabled
for
badbit,
a
bad
state
will
throw
std::ios_base::failure.
or
output
device,
such
as
a
hardware
failure,
a
lost
connection,
or
a
corrupted
internal
buffer.
It
can
also
occur
if
a
library
detects
data
integrity
problems
in
the
stream.
Handling
usually
involves
reporting
the
error
to
the
user
or
attempting
a
recovery
from
a
higher
level.
flags
used
to
describe
stream
readiness.
It
is
distinct
from
the
end-of-file
indicator
and
non-recoverable
logical
failures,
and
its
presence
signals
that
further
I/O
may
be
impossible.