开口时流不好

Not good stream at opening?

本文关键字:      更新时间:2023-10-16

当我以二进制模式打开文件时,是否存在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();
}

否:如果文件打开失败,则需要 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[文件成员]/2

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

返回

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

相关文章:
  • 没有找到相关文章