【发布时间】:2011-07-23 15:38:04
【问题描述】:
如果我有一个包含 5 个子文件夹的文件夹,并且我想在每个子文件夹中搜索某些文件(我的程序存在于主文件夹中)。如何让我的程序在 C++ 中进出这些文件夹?
我需要我的程序在 Windows 平台上运行。
谢谢!
【问题讨论】:
-
<filesystem> 已成为 C++17 标准;看看吧。
标签: c++ windows search traversal
如果我有一个包含 5 个子文件夹的文件夹,并且我想在每个子文件夹中搜索某些文件(我的程序存在于主文件夹中)。如何让我的程序在 C++ 中进出这些文件夹?
我需要我的程序在 Windows 平台上运行。
谢谢!
【问题讨论】:
标签: c++ windows search traversal
只需使用boost's recursive_directory_iterator,并过滤您想要的文件/目录。
boost::filesystem::recursive_directory_iterator iter("your\path");
boost::filesystem::recursive_directory_iterator end;
for (; iter != end; ++iter) {
// check for things like is_directory(iter->status()), iter->filename() ....
// optionally, you can call iter->no_push() if you don't want to
// enter a directory
// see all the possibilities by reading the docs.
}
【讨论】:
最明显的方法是使用FindFirstFile 和FindnextFile,以及SetCurrentDirectory。遍历子目录的一种明显方法是使目录遍历例程递归。
【讨论】:
boost::filesystem。 boost.org/doc/libs/1_46_0/libs/filesystem/v3/doc/index.htm
只需使用堆栈并实现深度优先搜索(参见 wiki)http://en.wikipedia.org/wiki/Depth-first_search
通过这种方式,您可以(使用尽可能小的堆栈)遍历任何树状结构(并且 Windows 的文件系统是树状的)。
【讨论】: