在 C++ 中,每个流都有一个 bad
位:
In C++, each stream has a bad
bit:
该标志由在读取或写入数据时发生错误时对流执行的操作设置,通常会导致流的完整性丢失.
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.
来源
什么会导致流失去完整性"并进入 bad
状态?这与 fail
状态不同,当输入流尝试将值存储到不能接受该值的变量中时(例如尝试将字符串存储到整数变量中),最常发生这种情况).
What would cause a stream to "lose integrity" and enter the bad
state? This is not the same as the fail
state, which most often occurs when an input stream attempts to store a value into a variable that cannot accept said value (such as attempting to store a string into an integer variable).
注意这个问题是c++文件坏位的更一般形式,即特定于文件输入流;这个问题不是一个完全重复的问题,因为它通常适用于输入和输出流.
Note that this question is a more general form of c++ file bad bit, which is specific to file input streams; this question is not an exact duplicate as it applies to both input and output streams in general.
根据 cppreference.com一个>:
标准库在以下情况下设置badbit:
The standard library sets badbit in the following situations:
通过 put()
或 write()
插入输出流失败原因.
Insertion into the output stream by put()
or write()
fails for any
reason.
通过 operator<<
、std::put_money
或std::put_time
,无法完成,因为输出结束流已到达(构面的格式化输出函数,例如num_put::put()
或 money_put::put()
,返回一个迭代器 iter
,例如iter.failed()==true
)
Insertion into the output stream by operator<<
, std::put_money
or
std::put_time
, could not complete because the end of the output
stream was reached (The facet's formatting output function such as
num_put::put()
or money_put::put()
, returns an iterator iter
such
that iter.failed()==true
)
Stream 是用 rdbuf()
的空指针构造的,或者putback()
/unget()
在带有空 rdbuf()
的流上调用,或者传递给operator<<(basic_streambuf*)
Stream is constructed with a null pointer for rdbuf()
, or
putback()
/unget()
is called on a stream with a null rdbuf()
, or a
null pointer passed to operator<<(basic_streambuf*)
rdbuf()->sputbackc()
或 rdbuf()->sungetc()
返回 traits::eof()代码>到
putback() 或
unget()`
rdbuf()->sputbackc()
or rdbuf()->sungetc()
return traits::eof()
to
putback() or
unget()`
rdbuf()->pubsync()
返回 -1 到 sync()
,到 flush()
,或到unitbuf
流上的 ostream::sentry
析构函数
rdbuf()->pubsync()
returns -1 to sync()
, to flush()
, or to the
destructor of ostream::sentry
on a unitbuf
stream
任何成员函数在 I/O 操作期间抛出异常关联的流缓冲区(例如 sbumpc()
、xsputn()
、sgetc()
、overflow()
, etc)
Exception is thrown during an I/O operation by any member function of
the associated stream buffer (e.g. sbumpc()
, xsputn()
, sgetc()
,
overflow()
, etc)
在iword()
或pword()
中抛出异常(例如std::bad_alloc
)
Exception is thrown in iword()
or pword()
(e.g. std::bad_alloc
)
<小时>
这可能是选择 cppreference.com 而不是 www.cpluplus.com 的另一个原因,请参阅:cplusplus.com 有什么问题?
这篇关于什么可能导致流进入“坏"?状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!