名前空間
変種
操作

std::basic_ios<CharT,Traits>::operator!

From cppreference.com
< cpp‎ | io‎ | basic ios
 
 
 
 
bool operator!() const;

関連ストリームでエラーが発生した場合に true を返します。具体的には、rdstate()badbit または failbit が設定されている場合に true を返します。

目次

[編集] パラメータ

(なし)

[編集] 返り値

エラーが発生した場合は 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";
}

[編集] 関連項目

以下の表は、すべての可能な 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
English 日本語 中文(简体) 中文(繁體)