C++查找指定目录下所以指定类型的文件

1 /***************************************************************
 2 函数名称:FindFile
 3 查找指定目录下指定文件
 4 输入:fileName:指定文件名,fileNath:指定查找路径
 5 输出:打印文件路径
 6 ***************************************************************/
 7 int FindFile(string fileName, string filePath)
 8 {
 9     assert(fileName != "" && filePath != "");
10     string exeName = fileName.substr(fileName.find_last_of('.'));
11     string strPath = filePath;
12     string filiterName = "*.*";
13     if ( strPath[strPath.length() - 1] != '\\')
14     {
15         strPath = strPath + "\\";
16     }
17     _finddata_t fileInfo;
18     long handle = _findfirst((strPath + filiterName).c_str(), &fileInfo);
19     if (handle == -1L)
20     {
21         cout<<"Cannot Open The Path!"<<endl;
22         return 0;
23     }
24     do
25     {
26         string path = fileInfo.name;
27         if (fileInfo.attrib & _A_SUBDIR)
28         {
29             if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0)
30             {
31                 FindFile(fileName, strPath + path + "\\");
32             }
33         }
34         else if (fileInfo.attrib & _A_ARCH && path.substr(path.find_last_of('.')) == exeName)
35         {
36             cout<<strPath + fileInfo.name<<endl;
37         }
38     }while (_findnext(handle, &fileInfo) == 0);
39     _findclose(handle);
40     return 0;
41 }

原文链接: https://www.cnblogs.com/tianyalangzi/p/3148691.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/92896

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午1:54
下一篇 2023年2月10日 上午1:54

相关推荐