导航

< 2025年8月 >
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

Windows C++ 非递归式(stack)深度优先遍历目录

Posted on 2014-07-11 12:40  codeape  阅读(1087)  评论(0)    收藏  举报
复制代码
 1 #include <Windows.h>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <stack>
 6 
 7 typedef void (__stdcall *P_WALK_DIR_CALLBACK)(const std::string &In_strFilePath);
 8 
 9 int WalkDir(const char *In_pcRoot, P_WALK_DIR_CALLBACK In_pfunCallBack)
10 {
11     int         iRetVal = 0;
12     std::string strRoot;
13     std::stack<std::string> stkDirs;
14 
15     if (In_pcRoot == NULL || In_pfunCallBack == NULL)
16     {
17         iRetVal = -1;
18         goto fun_ret;
19     }
20 
21     strRoot = In_pcRoot;
22     if (strRoot.empty())
23     {
24         iRetVal = -2;
25         goto fun_ret;
26     }
27 
28     if (strRoot.back() != '\\' && strRoot.back() != '/')
29         strRoot += '\\';
30     stkDirs.push(strRoot);
31 
32     while (!stkDirs.empty())
33     {
34         std::string         strDirForWalk;
35         WIN32_FIND_DATAA    Win32FindData   = {0};
36         HANDLE              hFindHandle     = NULL;
37 
38         strDirForWalk = stkDirs.top();
39         stkDirs.pop();
40         hFindHandle = FindFirstFileA((strDirForWalk + "*").c_str(), &Win32FindData);
41         if (hFindHandle == INVALID_HANDLE_VALUE)
42             continue;
43 
44         if (!(strlen(Win32FindData.cFileName) == 1 && strncmp(Win32FindData.cFileName, ".", 1) == 0)
45             && !(strlen(Win32FindData.cFileName) == 2 && strncmp(Win32FindData.cFileName, "..", 2) == 0))
46         {
47             if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
48                 stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\");
49             else
50                 In_pfunCallBack(strDirForWalk + Win32FindData.cFileName);
51         }
52         while (FindNextFileA(hFindHandle, &Win32FindData))
53         {
54             if (!(strlen(Win32FindData.cFileName) == 1 && strncmp(Win32FindData.cFileName, ".", 1) == 0)
55                 && !(strlen(Win32FindData.cFileName) == 2 && strncmp(Win32FindData.cFileName, "..", 2) == 0))
56             {
57                 if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
58                     stkDirs.push(strDirForWalk + Win32FindData.cFileName + "\\");
59                 else
60                     In_pfunCallBack(strDirForWalk + Win32FindData.cFileName);
61             }
62         }
63 
64         if (hFindHandle != INVALID_HANDLE_VALUE)
65             FindClose(hFindHandle);
66     }
67 
68 fun_ret:
69     return iRetVal;
70 }
71 
72 void inline __stdcall WalkDirCallBack(const std::string &In_strFilePath)
73 {
74     printf("%s\n", In_strFilePath.c_str());
75     return;
76 }
77 
78 void main(int argc, char **argv)
79 {
80     WalkDir(argv[1], WalkDirCallBack);
81     return;
82 }
复制代码

 

编辑推荐:
· 我最喜欢的 C# 14 新特性
· 程序员究竟要不要写文章
· 一个被BCL遗忘的高性能集合:C# CircularBuffer深度解析
· 如何正确实现一个 BackgroundService
· 抽象与性能:从 LINQ 看现代 .NET 的优化之道
阅读排行:
· 我最喜欢的 C# 14 新特性
· 一款基于 .NET + Vue 编写的仿钉钉的开源低代码工作流引擎,支持多种数据库,开箱即用!
· 免费的个人网站托管-InfinityFree
· 那些被推迟的 C# 14 特性及其背后的故事
· 餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
点击右上角即可分享
微信分享提示