0

我目前正在使用目录迭代器来读取整个目录,它做得很好,因为它读取了目录中任何子文件夹中的每个文件。

有没有办法知道它何时到达子文件夹的末尾并将返回到目录的主文件夹以开始下一个?

例如,如果我的目录有:

mainFOLDER>SUBFOLDER>ANOTHERSUB(this is the end)
mainFOLDER>difSUBFOLDER> so on so on
mainFOLDER> whatever

我怎么知道它何时击中“另一个子”。


        if (std::filesystem::is_directory(r)) {
            fs::path path = r.path();
            std::string path_string = path.u8string();
            if (inside == false) {
                parentDir = path_string;
            }

            double clests = processDir(path_string,inside);
            if (clests != -1) {
                string newString = to_string(clests);
                finishedPaths.push_back(newString + "   " + parentDir);
            }



        }
        else if (std::filesystem::is_regular_file(r)) {
            fs::path path = r.path();
            std::string path_string = path.u8string();
            double File = processFile(path_string);
            if (File != -1) {
                string newString = to_string(File);
                newString = newString + "   " + path_string;
                finishedPaths.push_back(newString);
            }

        }
    }
4

2 回答 2

0

在下面的代码中,我构建了使用std::filesystemAPI 来获取您所要求的内容。如果我误解了你的问题,请告诉我。

我将代码解释为注释。

#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;
/** Print each file in the directory, separate files in different directories
 * with a horizontal line.
 */
void example1() {
  for (auto &p : fs::recursive_directory_iterator(".")) {
    if (p.is_directory()) { // We could do something fancy and stop if the
                            // p.path() is what we are looking for.
      std::cout << "--------\n";
      continue;
    }
    std::cout << p.path() << '\n';
  }
}
/** Let's recurse until we went down the first path along the Depth-First path.
 *
 * The easy way to do this, is to stop as soon as the first file is found. (See
 * <TODO>)
 *
 * But with a custom loop, we can potentially do more.
 */
void example2() {
  auto it = fs::recursive_directory_iterator(".");
  while (it != end(it)) { // Note: fs::end(...) argument is ignored.
    std::cout << it->path() << "\t" << it.depth() << "\n";
    /* 1. it++: This moves to the next entry. That's the default thing to do.
     * 2. it.pop(). "moves the iterator one level up in the directory hierarchy"
     * This might be useful if we are looking for specific files, and want to
     * continue in the next directory.
     * 3. it.disable_recursion_pending(): It means that it won't go into
     * subdirectories.
     */
    it++; // Code style: If we always just increment it, for(it; it!=end(it);
          // it++) is better than a while loop.
  }
}
/** If I understood this correctly, you want to stop as soon you hit a directory
 * named 'anothersub'.
 */
void example3() {
  for (auto it = fs::recursive_directory_iterator("."); it != end(it); it++) {
    const auto p = it->path();
    if (it->is_directory() and p.filename() == "anothersub") {
      break;
    }
    std::cout << p << "\n";
  }
}
int main() {
  example1();
  example2();
  example3();
}

https://en.cppreference.com/w/cpp/filesystem/recursive_directory_iterator

https://en.cppreference.com/w/cpp/filesystem/directory_entry

于 2020-04-16T05:01:36.477 回答
0

你应该使用

auto depth = it.depth(); 

跟踪它的深度并调用

it.pop(); 

当 depth > 2 时退出不需要的目录并继续您期望的位置。

于 2021-09-20T22:19:58.427 回答