Namespaces
Variants

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

From ja.cppreference.net
bool good ( ) const ;

ストリームに対する直近のI/O操作が正常に完了した場合に true を返します。具体的には、 rdstate ( ) == 0 の結果を返します。

ストリームステータスビットを設定する条件の一覧については、 ios_base::iostate を参照してください。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - HTMLタグ、属性、クラス名はすべて保持されています - ``内のテキスト(Parameters、Return value、Example、See also)はC++関連の専門用語として翻訳せずに保持しています - 番号や構造はすべて元のまま維持されています

パラメータ

(なし)

戻り値

true ストリームのエラーフラグがすべてfalseの場合、 false それ以外の場合。

例

#include <cstdlib>
#include <fstream>
#include <iostream>
int main()
{
    const char* fname = "/tmp/test.txt";
    std::ofstream ofile{fname};
    ofile << "10 " << "11 " << "12 " << "non-int";
    ofile.close();
    std::ifstream file{fname};
    if (!file.good())  
    {  
        std::cout << "#1. Opening file test.txt failed - "
                     "one of the error flags is true\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 << "#2. I/O error while reading - badbit is true\n";
        return EXIT_FAILURE;
    } 
    else if (file.eof())
        std::cout << "#3. End of file reached successfully - eofbit is true\n"
            "This is fine even though file.good() is false\n"; 
    else if (file.fail())
        std::cout << "#4. Non-integer data encountered - failbit is true\n";
}

出力例:

10 11 12 
#4. Non-integer data encountered - failbit is true

関連項目

以下の表は、すべての可能な basic_ios アクセサ( good() 、 fail() など)の値を、 ios_base::iostate フラグの全組み合わせについて示しています:

**注記**: このHTMLフラグメントには翻訳すべきテキストが含まれていません。すべてのコンテンツは以下のいずれかに該当します: - HTMLタグと属性(翻訳対象外) - ``タグ内のC++用語(`eofbit`, `failbit`, `badbit` - 翻訳対象外) - 関数名と演算子(`good()`, `fail()`, `bad()`, `eof()`, `operator bool`, `operator!` - 翻訳対象外) C++のストリーム状態フラグとメンバ関数は技術用語のため、原文のまま保持することが適切です。 **注記**: このHTMLテーブル行にはC++のブール値リテラル「true」と「false」が含まれており、これらはC++固有の用語として翻訳対象外です。HTMLタグ、属性、および書式設定はすべて元のまま保持されています。
ios_base::iostate フラグ basic_ios アクセサ
eofbit failbit badbit good() fail() bad() eof() operator bool operator!
false false false true false false false true false
偽 偽 真 偽 真 真 偽 偽 真
偽 真 偽 偽 真 偽 偽 偽 真
偽 真 真 偽 真 真 偽 偽 真
true false false false false false true true false
true false true false true true true false true
真 真 偽 偽 真 偽 真 偽 真
true true true false true true true false true