c++ 判断文件是否存在

如题所述

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(int argc,char *argv[])
{
    string ifile("c:\\c.txt"),tmp;
    ifstream ifs(ifile.c_str());
    if(!ifs)
    {
        cout<<"不存在"<<endl;
    }
    else cout<<"存在"<<endl;
    return 0;
}

判断C盘下C.txt是否存在

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-12-18
bool FileExist(char* strPrePath, const char* strFileName)
{
    CFileFind cFindfile;
    char strPath[2*256];
    sprintf(strPath, "%s%s%s", strPrePath, "\\", strFileName);
    
    return cFindfile.FindFile(strPath);
}

// strPrePath是文件所在路径不包括文件名  strFileName是文件名

第2个回答  2013-12-18
用FindFile函数
相似回答