在线文档教程
C++
输入/输出 | Input/output

std::basic_ios::bad

性病:基本[医]监督办::坏

bool bad() const;

回报true如果关联的流上发生了不可恢复的错误。具体来说,返回true如果badbit设为rdstate()...

ios_base::iostate的条件列表badbit...

参数

%280%29

返回值

true如果发生了不可恢复的错误,false否则。

二次

#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"; }

二次

另见

下表显示basic_ios访问器%28good(),,,fail()的所有可能组合的%29。ios_base::iostate旗帜:

ios_base::iostate flagsbasic_ios accessors
eofbitfailbit
falsefalse
falsefalse
falsetrue
falsetrue
truefalse
truefalse
truetrue
truetrue

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]IOS/BAD