簡體   English   中英

使用純c ++(跨平台)獲取特定路徑中的所有目錄/文件和子目錄

[英]Get all directories/files and sub-directories in a specific path using pure c++ (cross-platform)

如何使用現代C ++獲取特定路徑中目錄/文件和子目錄的向量?

std::filesystem使用遞歸函數..!

#if __cplusplus < 201703L// If the version of C++ is less than 17
// It was still in the experimental:: namespace
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#include <filesystem>
namespace fs = std::filesystem;
#endif
std::vector<fs::path> geteverything(const fs::path &path)
{
    std::vector<fs::path> dirs;
    for (const auto & entry : fs::directory_iterator(path))
    {
        dirs.push_back(entry);
        if (fs::is_directory(entry))
        {
            auto subdirs = geteverything(entry);
            dirs.insert(dirs.end(), subdirs.begin(), subdirs.end());
        }
    }
    return dirs;
}

測試

// Change this to the absolute/relative path you'd like to fetch.
std::string path = "C:/Windows/Temp";
int main()
{
    std::cout << "fetching all directories and files in : " << path << " ...\n";
    auto list = geteverything(path);
    for (const auto &path : list)
        std::cout << path << "\n";
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM