std::basic_ios<CharT,Traits>::fail
From cppreference.com
| bool fail() const; |
||
関連ストリームでエラーが発生した場合にtrueを返します。具体的には、rdstate()でbadbitまたはfailbitが設定されている場合にtrueを返します。
failbitまたはbadbitを設定する条件のリストについては、ios_base::iostateを参照してください。
目次 |
[編集] パラメータ
(なし)
[編集] 戻り値
エラーが発生した場合はtrue、それ以外の場合はfalse。
[編集] 例
このコードを実行
#include <cstdlib> #include <fstream> #include <iostream> 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 のアクセサ(good()、fail()など)が、ios_base::iostate フラグのすべての可能な組み合わせに対してどのような値を返すかを示す表を以下に示します。
| ios_base::iostate フラグ | basic_ios アクセサ | |||||||
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 |
| ファイルエラーをチェックする (関数) |