MFC查找指定文件是否存在

MFC
1124
2023/7/26 19:01:39
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

您可以使用CFileFind类来查找指定文件是否存在。以下是一个示例代码:

#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函数接受一个LPCTSTR参数表示文件路径,然后使用CFileFind的FindFile方法尝试查找指定文件。如果返回值为true,则表示文件存在,否则表示文件不存在。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: mfc怎么用进度条显示算法进度