【问题标题】:use findfirstfile and findnextfile to search in subdirectories使用 findfirstfile 和 findnextfile 在子目录中搜索
【发布时间】:2020-07-28 17:14:57
【问题描述】:

在 C++ 世界中相当新,因为我更熟悉 C#。我设法让 findfirstfile 和 findnextfile 工作,但我想知道是否有一种简单的方法可以通过不同的驱动器甚至只是子目录进行搜索。

谢谢!

【问题讨论】:

标签: c++ file directory


【解决方案1】:

是的,尝试使用std::filesystem,例如这样

    std::string tasksFolderName = "/home/user";
    std::experimental::filesystem::directory_iterator fit(tasksFolderName);
    for (auto& f : fit) {
        if (std::experimental::filesystem::is_directory(f.path())) {
            // ...
        }
    }

【讨论】:

    【解决方案2】:
    for (auto& p : std::filesystem::directory_iterator(<Directory to iterate>))
        std::cout << p.path() << '\n';
    

    您可以使用上面的代码。要使用它,您需要一个支持 c++17 的编译器。 您可以阅读更多关于 c++17 中引入的 Filesystem library 的信息。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 2013-02-20
      • 2011-12-25
      • 2021-01-20
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多