名前空間
変種
操作

std::basic_ios<CharT,Traits>::bad

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

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

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";
}

[編集] 関連項目

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