c++ 判断文件是否存在

2026年09月23日 11:23
有3个网友回答
网友(1):

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

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

网友(2):

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是文件名

网友(3):

用FindFile函数