也许是一个虚拟问题,但我需要一个明确的答案。这些函数的返回有什么区别吗
int FileExists(const std::string& filename)
{
ifstream file(filename.c_str());
return !!file;
}
int FileExists(const std::string& filename)
{
ifstream file(filename.c_str());
return file.is_open();
}
换句话说,我的问题是:是否铸造fstream
to bool
给出完全相同的结果fstream::is_open()
?
No. is_open
仅检查是否存在关联文件,而强制转换为bool
还检查文件是否已准备好进行 I/O 操作(例如,流处于良好状态)(C++11 起)。
is_open http://en.cppreference.com/w/cpp/io/basic_ifstream/is_open
检查文件流是否有关联的文件。
std::basic_ios::operator bool http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
如果流没有发生错误并且准备好进行 I/O 操作,则返回 true。具体来说,返回!fail()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)