std::basic_ios<CharT,Traits>::operator!
提供: cppreference.com
<tbody>
</tbody>
bool operator!() const; |
||
紐付けられたストリームにエラーが発生していれば true を返します。 具体的には、 rdstate() の badbit または failbit がセットされていれば true を返します。
引数
(なし)
戻り値
エラーが発生していれば 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";
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";
}
関連項目
以下の表は 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 |