std::basic_ios::bad
来自cppreference.com
![]() |
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
bool bad() const; |
||
返回true不可恢复的错误发生在相关的数据流。具体而言,回报true如果
badbit
在rdstate().原文:
Returns true if non-recoverable error has occurred on the associated stream. Specifically, returns true if
badbit
is set in rdstate().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 iteratoriter
such that iter.failed()==true) - 流构造一个空指针rdbuf(),,或补篮()/ unget()被称为一个空rdbuf(),或一个空指针传递给运营商“(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()返回性状:: EOF()补篮()或unget()原文:rdbuf()->sputbackc() or rdbuf()->sungetc() return traits::eof() to putback() or unget()
- rdbuf() - > pubsync(,)返回-1同步(),flush()函数或析构函数的ostream ::哨兵一unitbuf流原文:rdbuf()->pubsync() returns -1 to sync(), to flush(), or to the destructor of ostream::sentry on a unitbuf stream
- 抛出异常的I / O操作过程中的任何成员函数相关的数据流缓冲(例如sbumpc xsputn(),(),,sgetc(),溢出(),等)原文: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)
目录 |
[编辑] 参数
(无)
[编辑] 返回值
true如果发生不可恢复的错误,false否则.
原文:
true if a non-recoverable error has occurred, false otherwise.
[编辑] 示例
#include <iostream> #include <fstream> #include <cstdlib> int main() { std::ifstream file("test.txt"); if(!file) // operator! is used here { std::cout << "File opening failed\n"; return EXIT_FAILURE; } // typical C++ I/O loop uses the return value of the I/O function // as the loop controlling condition, operator bool() is used here for(int n; file >> n; ) { std::cout << n << ' '; } std::cout << '\n'; if (file.bad()) std::cout << "I/O error while reading\n"; else if (file.eof()) std::cout << "End of file reached successfully\n"; else if (file.fail()) std::cout << "Non-integer data encountered\n"; }
[编辑] 另请参阅
ios_base::iostate flags | basic_ios accessors | |||||||
eofbit | failbit | badbit | good() | fail() | bad() | eof() | operator bool() | operator!() |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |