gpt4 book ai didi

c++ - 开场流量不好?

转载 作者:行者123 更新时间:2023-11-28 07:55:41 25 4
gpt4 key购买 nike

当我以二进制模式打开文件时,是否存在 is_open()truegood() ?

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 的双参数构造函数文件打开失败需要设置failbit。

§27.9.1.7[ifstream.cons]/2

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

calls rdbuf()->open(s, mode | ios_base::in). If that function returns a null pointer, calls setstate(failbit).

并且,对于 open(),

§27.9.1.4[filebuf.members]/2

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

Returns: this if successful, a null pointer otherwise

关于c++ - 开场流量不好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com