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

std::basic_ios::fail

性病:基本[医]监督办::失败

bool fail() const;

回报true如果关联流上发生错误,则为。具体来说,返回true如果badbitfailbit设为rdstate()...

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

参数

%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

ferrorchecks for a file error (function)

© cppreference.com

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

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