0

ファイルをバイナリ モードで開いたときに、あるのにあるという状況はis_open()ありますか?truegood()false

bool ok = false;
std::ifstream stream("test.dat", std::ios::binary)
if (stream.is_open())
{
    ok = stream.good();//Does a situation exist where the result of this is false ?
    stream.close();
}
4

1 に答える 1

1

いいえ:std::ifstreamファイルのオープンに失敗した場合、failbit を設定するには の 2 つの引数のコンストラクターが必要です。

§27.9.1.7[ifstream.cons]/2

explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);

を呼び出しますrdbuf()->open(s, mode | ios_base::in)。その関数が null ポインタを返す場合、 を呼び出しますsetstate(failbit)

そして、open() の場合、

§27.9.1.4[filebuf.members]/2

basic_filebuf<charT,traits>* open(const char* s, ios_base::openmode mode);

戻り値:this成功した場合、それ以外の場合は null ポインター

于 2012-10-08T02:12:31.560 に答える