0

当我以二进制模式打开文件时,是否存在is_open()is truebut good()is的情况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如果文件打开失败,则需要两个参数的构造函数来设置失败位。

§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)。如果该函数返回一个空指针,则调用setstate(failbit).

并且,对于 open(),

§27.9.1.4[filebuf.members]/2

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

返回:this如果成功,则返回空指针

于 2012-10-08T02:12:31.560 回答