我正在使用下面的一段代码来检查文件是否存在或使用std::ifstream
。std :: ifstream初始化文件名
#include <iostream>
#include <fstream>
int main()
{
if (std::ifstream("file.txt"))
{
std::cout << "File present\n";
}
return 0;
}
因为我没有使用任何std::ifstream
对象,我怎么关闭文件?或者不需要致电关闭?
该文件自动关闭(您正在使用*临时*对象)。 – Leon
无关的评论,但不要依赖于这个重要的东西。确保存在文件的唯一方法是打开它并抓住手柄。因为否则另一个程序可能会在您检查后删除文件。 https://en.wikipedia.org/wiki/Time_of_check_to_time_of_use –