可能导致流进入“不良"状态的原因是什么?状态? [英] What could cause a stream to enter the "bad" state?
问题描述
在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
状态不同,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()
orwrite()
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
)
使用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() or
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()
等)
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
)
这可能是选择cppreference.com而不是www.cpluplus.com的另一个原因,请参见: cplusplus.com怎么了?
This may be one more reason to choose cppreference.com over www.cpluplus.com, see: What's wrong with cplusplus.com?
这篇关于可能导致流进入“不良"状态的原因是什么?状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!