溫馨提示×

MFC查找指定文件是否存在

MFC
小云
127
2023-08-05 19:01:39
欄目: 編程語言

您可以使用CFileFind類來查找指定文件是否存在。以下是一個(gè)示例代碼:

#include <afx.h>
bool IsFileExists(LPCTSTR filePath)
{
CFileFind fileFind;
return fileFind.FindFile(filePath);
}
int main()
{
LPCTSTR filePath = _T("C:\\path\\to\\file.txt");
if (IsFileExists(filePath))
{
printf("File exists\n");
}
else
{
printf("File does not exist\n");
}
return 0;
}

在上述代碼中,IsFileExists函數(shù)接受一個(gè)LPCTSTR參數(shù)表示文件路徑,然后使用CFileFind的FindFile方法嘗試查找指定文件。如果返回值為true,則表示文件存在,否則表示文件不存在。

0