名前空間
変種
操作

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

提供: cppreference.com
< cpp‎ | io‎ | basic ios
 
 
入出力ライブラリ
入出力マニピュレータ
Cスタイルの入出力
バッファ
(C++98で非推奨)
ストリーム
抽象
ファイル入出力
文字列入出力
配列入出力
(C++98で非推奨)
(C++98で非推奨)
(C++98で非推奨)
同期化出力
エラーカテゴリインタフェース
(C++11)
 
 
bool fail() const;

紐付けられたストリームにエラーが発生していれば true を返します。 具体的には、 rdstate()badbit または failbit がセットされていれば true を返します。

failbit または badbit がセットされる状況の一覧は ios_base::iostate を参照してください。

目次

[編集] 引数

(なし)

[編集] 戻り値

エラーが発生していれば true、そうでなければ false

[編集]

#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
ファイルのエラーを調べます
(関数) [edit]