zoukankan      html  css  js  c++  java
  • 广度优先遍历目录(Windows平台、C++)

    深度优先的遍历网上一大把,就是递归调用,这里就不说了,说点网上找不到的。

     1 #include <Windows.h>
     2 #include <stdint.h>
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <string>
     6 #include <queue>
     7 
     8 typedef int8_t (__stdcall *P_WALKDIR_CALLBACK)(const char *In_pcFilePath);
     9 
    10 int8_t WalkDir(const char *In_pcRootDir, P_WALKDIR_CALLBACK In_pfunCallBack)
    11 {
    12     int8_t          i8RetVal = 0;
    13     std::string     strLocalRoot;
    14     std::queue<std::string> qDirectory;
    15 
    16     if (In_pcRootDir == NULL || In_pfunCallBack == NULL)
    17     {
    18         i8RetVal = -1;
    19         goto fun_ret;
    20     }
    21 
    22     strLocalRoot = In_pcRootDir;
    23     if (strLocalRoot.empty())
    24     {
    25         i8RetVal = -2;
    26         goto fun_ret;
    27     }
    28 
    29     char cRootBack = strLocalRoot.back();
    30     if (cRootBack != '\' && cRootBack != '/')
    31     {
    32         strLocalRoot += '\';
    33     }
    34     qDirectory.push(strLocalRoot);
    35 
    36     do 
    37     {
    38         std::string     strDirForWalk   = qDirectory.front();
    39         WIN32_FIND_DATA Win32FindData   = {0};
    40         HANDLE          hFindHandle     = NULL;
    41 
    42         qDirectory.pop();
    43         hFindHandle = FindFirstFile((strDirForWalk + "*").c_str(), &Win32FindData);
    44         if (hFindHandle == INVALID_HANDLE_VALUE)
    45         {
    46             continue;
    47         }
    48         if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
    49         {
    50             if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    51             {
    52                 qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\");
    53             }
    54             else
    55             {
    56                 In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
    57             }
    58         }
    59 
    60         while (FindNextFile(hFindHandle, &Win32FindData))
    61         {
    62             if (strcmp(Win32FindData.cFileName, ".") != 0 && strcmp(Win32FindData.cFileName, "..") != 0)
    63             {
    64                 if (Win32FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    65                 {
    66                     qDirectory.push(strDirForWalk + Win32FindData.cFileName + "\");
    67                 }
    68                 else
    69                 {
    70                     In_pfunCallBack((strDirForWalk + Win32FindData.cFileName).c_str());
    71                 }
    72             }
    73         }
    74 
    75         if (hFindHandle != NULL)
    76         {
    77             FindClose(hFindHandle);
    78         }
    79     } while (!qDirectory.empty());
    80 
    81 fun_ret:
    82     return i8RetVal;
    83 }
    84 
    85 int8_t __stdcall WalkDirCallBack(const char *In_pcFilePath)
    86 {
    87     if (In_pcFilePath != NULL)
    88     {
    89         printf("%s
    ", In_pcFilePath);
    90     }
    91     return 0;
    92 }
    93 
    94 void main(int argc, char **argv)
    95 {
    96     WalkDir(argv[1], WalkDirCallBack);
    97     return;
    98 }

    PS:

    用Python测试遍历结果正确性,发现Python是深度优先的遍历,呵呵。

  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 高精度乘法
    Java实现 蓝桥杯VIP 算法提高 高精度乘法
    Java实现 蓝桥杯VIP 算法提高 高精度乘法
    Java实现 蓝桥杯VIP 算法提高 高精度乘法
    Java实现 蓝桥杯VIP 算法提高 高精度乘法
    Java实现 蓝桥杯VIP 算法提高 现代诗如蚯蚓
    Java实现 蓝桥杯VIP 算法提高 现代诗如蚯蚓
    Java实现 蓝桥杯VIP 算法提高 现代诗如蚯蚓
    Java实现 蓝桥杯VIP 算法提高 现代诗如蚯蚓
    ddd
  • 原文地址:https://www.cnblogs.com/codeape/p/3250395.html
Copyright © 2011-2022 走看看