std::basic_ios::fail
性病:基本[医]监督办::失败
bool fail() const; | | |
---|
回报true
如果关联流上发生错误,则为。具体来说,返回true
如果badbit
或failbit
设为rdstate()
...
见ios_base::iostate
的条件列表failbit
或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 flags | basic_ios accessors |
---|---|
eofbit | failbit |
false | false |
false | false |
false | true |
false | true |
true | false |
true | false |
true | true |
true | true |
ferror | checks for a file error (function) |
---|
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。