This flag is set by operations performed on the stream when an error occurs while read or writing data,generally causing the loss of integrity of the stream.
什么会导致流“失去诚信”并进入坏状态?这与失败状态不同,这通常在输入流尝试将值存储到不能接受所述值的变量(如尝试将字符串存储到整数变量中)时发生.
请注意,这个问题是c++ file bad bit的一个更通用的形式,它特定于文件输入流;这个问题不是一个完全重复的,因为它一般适用于输入和输出流.
解决方法
The standard library sets badbit in the following situations:
Insertion into the output stream by
put()@H_502_23@ or
write()@H_502_23@ fails for any
reason.Insertion into the output stream by
operator<<@H_502_23@,
std::put_money@H_502_23@ or
std::put_time@H_502_23@,could not complete because the end of the output
stream was reached (The facet’s formatting output function such as
num_put::put()@H_502_23@ or
money_put::put()@H_502_23@,returns an iterator
iter@H_502_23@ such
thatiter.Failed()==true@H_502_23@)
Stream is constructed with a null pointer for
rdbuf()@H_502_23@,or
putback()@H_502_23@/
unget()@H_502_23@ is called on a stream with a null
rdbuf()@H_502_23@,or a
null pointer passed tooperator<<(basic_streambuf*)@H_502_23@
rdbuf()->sputbackc()@H_502_23@ or
rdbuf()->sungetc()@H_502_23@ return
traits::eof()@H_502_23@ to
putback() or@H_502_23@unget()`
rdbuf()->pubsync()@H_502_23@ returns -1 to
sync()@H_502_23@,to
flush()@H_502_23@,or to the
destructor ofostream::sentry@H_502_23@ on a
unitbuf@H_502_23@ stream
Exception is thrown during an I/O operation by any member function of
the associated stream buffer (e.g.sbumpc()@H_502_23@,
xsputn()@H_502_23@,
sgetc()@H_502_23@,
overflow()@H_502_23@,etc)
Exception is thrown in
iword()@H_502_23@ or
pword()@H_502_23@ (e.g.
std::bad_alloc@H_502_23@)
这可能是在www.cpluplus.com上选择cppreference.com的另一个原因,请参阅:
What’s wrong with cplusplus.com?