【问题标题】:How do I know when I found the end of the sub folder directory? c++我如何知道何时找到子文件夹目录的末尾? C++
【发布时间】:2020-04-15 16:05:55
【问题描述】:

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

有没有办法知道它什么时候到达子文件夹的末尾,然后会回到目录的主文件夹开始下一个?

例如,如果我的目录有:

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);
            }

        }
    }

【问题讨论】:

标签: c++ c++17 std std-filesystem


【解决方案1】:

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

我将代码解释为 cmets。

#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

【讨论】:

    【解决方案2】:

    你应该使用

    auto depth = it.depth(); 
    

    跟踪它的深度并调用

    it.pop(); 
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2012-11-12
      相关资源
      最近更新 更多