我想知道是否有任何功能或方法可以首先检查文件是否已创建

I want to know is there any function or method to check first either file is created or not

本文关键字:是否 检查 创建 文件 方法 想知道 任何 功能      更新时间:2023-10-16

我想知道是否有任何函数或方法可以首先检查文件是否已创建,如果它已创建,则读取信息,否则跳过文件读取过程。

bool file_does_exist(const std::string &filename) {
    return std::ifstream(filename);
}

您只需打开文件即可读取

std::fstream fs;
fs.open ("test.txt", std::fstream::in);

然后检查一切是否正常

if(fs.good())
   //...

这个问题不是太清楚,但你的意思是这个吗?

std::ifstream file("file_name.ext");
if (file)
{
  read_from(file);
}