作者:若涵她娘_124 | 来源:互联网 | 2023-09-17 08:54
wxWidgets非递归方式遍历文件在wxDir类中,wxWidgets已经给我们提供了wxDir::GetAllFiles函数和wxDir::Traverse来深度遍历某
wxWidgets 非递归方式遍历文件
在 wxDir 类中, wxWidgets已经给我们提供了 wxDir::GetAllFiles 函数 和 wxDir::Traverse 来深度遍历某个文件夹下的所有文件, 这里提供的是一种通过 wxDir::GetFirst 和 wxDir::GetNext 方法实现的非递归遍历方案, 其思路如下:
1>. 建立一个空的目录路径队列;
2>. 将待遍历的根目录路径添加到目录队列中;
3>. 取出队列前端的路径进行遍历, 若遇到的是目录则将其添加到目录队列中, 否则输出;
4>. 当队列前端的目录遍历完成后将其从队列中删除;
5>. 重复 3 - 4 步骤的操作, 直至目录队列为空。
用 C 语言代码描述:
1 #include
2 #include "wx/filefn.h"
3 #include "wx/dir.h"
4
5
6 int GetAllFilePath( wxString rootPath )
7 {
8 wxArrayString dirItems; //目录队列
9 dirItems.Add( rootPath ); //将待遍历的目录路径添加到目录队列中
10
11 wxString filename; bool cont; wxString tempPath;
12 while( dirItems.GetCount() ) //当目录队列不为空时执行
13 {
14 wxDir dir( dirItems[0] ); //打开队列前端目录路径
15 if( !dir.IsOpened() )
16 return -1;
17
18 cOnt= dir.GetFirst( &filename ); //获取首个文件
19 while ( cont )
20 {
21 tempPath = dirItems[0] + "\\" + filename; //合成 filename 的完整路径
22 if( dir.Exists( tempPath ) ) //判断 tempPath 是否为目录
23 dirItems.Add( tempPath ); //为目录时则添加到目录队列
24 else
25 printf("%s\n", tempPath ); //否则输出该文件路径
26
27 cOnt= dir.GetNext(&filename); //获取下一个文件
28 }
29 dirItems.RemoveAt(0); //当前目录遍历完成后将其从目录队列中删除
30 }
31
32 return 1;
33 }
34
35
36 int main()
37 {
38 GetAllFilePath( _T("D:\\Project\\wxWidgets") );
39
40 return 0;
41 }
更多关于 wxWidgets 文件/文件夹的内容:
[译] wxWidgets - File functions - 文件/文件夹函数
[代码分享] wxWidgets - wxDir 遍历文件
[译] wxWidgets - wxDir
--------------------
wid, 2013.03.08
上一篇: [代码分享] wxWidgets - wxDir 遍历文件