1. 判断文件是否存在
例如:#include <fstream> // fstream
std::fstream _file;
_file.open(g_sFaceFeaturePath.c_str(),ios::in);
if(!_file)
{
cout<< "Cannot find file:"<<endl;
exit(-1);
}
remove("./1.txt"); // 删除文件, #include "stdlib.h" //atoi
2.判断路径或文件夹是否存在
例如:#include <dirent.h> //DIR *dir
string g_sSamplePath = "./train/";
DIR *dir;
if ((dir=opendir(g_sSamplePath.c_str())) == NULL)
{
system("mkdir -p ./train/"); //system( s.c_str() );
}
system("rm -rf ./Result"); //删除文件夹
system("mkdir -p ./Result"); //创建文件夹
例如:
vector<string> vsFileName;
GetDirAllFile("./train", "", vsFileName); //获得文件夹的名字
GetDirAllFile("./train", ".bmp", vsFileName); //获得文件的名字
void CFaceProcess::GetDirAllFile(string sPath, string sTemplate, vector<string> &_vsFileName)
{
DIR *dir;
struct dirent *ptr;
_vsFileName.clear();
string s = " ";
string ss = " ";
int n = sTemplate.length();
if ((dir=opendir(sPath.c_str())) == NULL)
{
perror("Open dir error...");
exit(1);
}
// readdir() return next enter point of directory dir
while ((ptr=readdir(dir)) != NULL)
{
s = ptr->d_name; //cout<<s<<endl;
if (n > 0)
{
if (n < s.length())
{
ss = s.substr(s.length()-n); //从索引11往后的子串
if (!ss.compare(sTemplate)) //==
_vsFileName.push_back(s);
}
}
else
{
if (s.compare(".") && s.compare("..")) //compare() 返回true,表示等于
{
_vsFileName.push_back(s);
}
}
}
closedir(dir);
}