std::basic_ios<CharT,Traits>::bad
提供: cppreference.com
bool bad()const; | ||
紐付けられているストリームに回復不可能なエラーが発生していれば true を返します。 具体的には、 rdstate() の badbit がセットされていれば true を返します。
badbit
がセットされる状況の一覧は ios_base::iostate を参照してください。
目次 |
[編集]引数
(なし)
[編集]戻り値
回復不可能なエラーが発生していれば true、そうでなければ false。
[編集]例
Run this code
#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";returnEXIT_FAILURE;} // typical C++ I/O loop uses the return value of the I/O function// as the loop controlling condition, operator bool() is used herefor(int n; file >> n;){std::cout<< n <<' ';}std::cout<<'\n'; if(file.bad())std::cout<<"I/O error while reading\n";elseif(file.eof())std::cout<<"End of file reached successfully\n";elseif(file.fail())std::cout<<"Non-integer data encountered\n";}
[編集]関連項目
以下の表は ios_base::iostate フラグのすべての有り得る組み合わせに対する basic_ios
のアクセサ (good()、 fail() など) の値を示します。
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 |