C++判断文件是否存在

#include 
using namespace std;

/// 判断文件是否存在

int main()
{
    string dirPath;
    dirPath="/home/XXX/.txt"; //你的路径 什么类型的文件都可以
    // 进行判断
    ifstream txt_file(dirPath);
     if (!txt_file)
      {
           continue;  // 如果没有文件,选择跳过或其他的操作
       }
       else
       {
           //做你想要的处理
        }
    return 0;
}

判断你要查找的路径下有没有文件,使用 ifstream 进行判断即可。

你可能感兴趣的:(代码训练,c++)