c – 什么可能导致流进入“坏”状态?

c – 什么可能导致流进入“坏”状态?,第1张

概述在C中,每个流都有一个坏的位: 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. Source 什么会导致流“失去诚信”并进入坏状态 在C中,每个流都有一个坏的位:

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.

Source

什么会导致流“失去诚信”并进入坏状态?这与失败状态不同,这通常在输入流尝试将值存储到不能接受所述值的变量(如尝试将字符串存储到整数变量中)时发生.

请注意,这个问题是c++ file bad bit的一个更通用的形式,它特定于文件输入流;这个问题不是一个完全重复的,因为它一般适用于输入和输出流.

解决方法 根据 cppreference.com:

The standard library sets badbit in the following situations:

Insertion into the output stream by put() or write() fails for any
reason.

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 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() or rdbuf()->sungetc() return traits::eof() to
putback() orunget()`

rdbuf()->pubsync() returns -1 to sync(),to flush(),or to the
destructor of ostream::sentry on a unitbuf stream

Exception is thrown during an I/O operation by any member function of
the associated stream buffer (e.g. sbumpc(),xsputn(),sgetc(),
overflow(),etc)

Exception is thrown in iword() or pword() (e.g. std::bad_alloc)

这可能是在www.cpluplus.com上选择cppreference.com的另一个原因,请参阅:
What’s wrong with cplusplus.com?

总结

以上是内存溢出为你收集整理的c – 什么可能导致流进入“坏”状态?全部内容,希望文章能够帮你解决c – 什么可能导致流进入“坏”状态?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1254472.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存