在 C++ wxDir 中非递归地迭代目录
Iterate directories non-recursively in c++ wxDir
我正在尝试迭代目录并列出其中的所有文件夹。
我检查了 wx documentation 并使用了 GetAllFiles
目前我使用:
wxArrayString files;
size_t n, i;
n = wxDir::GetAllFiles("c:/temp/", &files, wxEmptyString, wxDIR_DIRS);
for(i=0; i<n; i++) {
myOwnPrint("folders: %s", files.Item(i));
}
它列出了所有目录和其中的子目录..
我想要的只是列出所有外部文件夹..
我检查了 wxDirFlags 但我认为没有仅非递归地列出目录的标志。
有什么想法吗?
GetAllFiles()
是一种辅助方法,可以避免在进行递归目录遍历时定义 wxDirTraverser
派生的 class,因此它的作用不大它不递归的意义——这就是它的目的。
对于给定目录中文件 and/or 目录的简单迭代,只需使用 GetFirst()
和 GetNext()
,正如评论中已经提到的那样:
wxDir dir(path);
if ( !dir.IsOpened() ) {
... handle error ...
}
wxString subdir;
for ( bool cont = dir.GetFirst(&subdir, wxString(), wxDIR_DIRS);
cont;
cont = dir.GetNext(&subdir) ) {
... do whatever you need to do with subdir ...
}
我正在尝试迭代目录并列出其中的所有文件夹。
我检查了 wx documentation 并使用了 GetAllFiles
目前我使用:
wxArrayString files;
size_t n, i;
n = wxDir::GetAllFiles("c:/temp/", &files, wxEmptyString, wxDIR_DIRS);
for(i=0; i<n; i++) {
myOwnPrint("folders: %s", files.Item(i));
}
它列出了所有目录和其中的子目录..
我想要的只是列出所有外部文件夹..
我检查了 wxDirFlags 但我认为没有仅非递归地列出目录的标志。
有什么想法吗?
GetAllFiles()
是一种辅助方法,可以避免在进行递归目录遍历时定义 wxDirTraverser
派生的 class,因此它的作用不大它不递归的意义——这就是它的目的。
对于给定目录中文件 and/or 目录的简单迭代,只需使用 GetFirst()
和 GetNext()
,正如评论中已经提到的那样:
wxDir dir(path);
if ( !dir.IsOpened() ) {
... handle error ...
}
wxString subdir;
for ( bool cont = dir.GetFirst(&subdir, wxString(), wxDIR_DIRS);
cont;
cont = dir.GetNext(&subdir) ) {
... do whatever you need to do with subdir ...
}