534
技術社區[雲棲]
C++中判斷一個文件是否存在的方法
最進在寫的一個係統需要保存結果數據,但是保存結果數據的時候,如果那個數據已經存在,就會無法保存,所以就需要先判斷是否存在該數據,判斷方法如下:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char *savePath = "/home/zhuwei/contour/linearIteration.shp";
fstream f2;
f2.open(savePath);
if(!f2)
{
cout<<"文件不存在"<<endl;
f2.close();
}
else
{
cout<<"文件存在"<<endl;
f2.close();
}
return 0;
}
編譯:g++ -o fileexists fileexisttest.cpp
運行:./fileexists
OK,可以運行
最後更新:2017-04-02 00:06:55