仅列出目录中的文件夹

问题描述:

我想列出C ++目录中的文件夹,最好以可移植(在主要操作系统上工作)的方式列出.我尝试使用POSIX,它可以正常工作,但是如何识别找到的项目是否是文件夹?

I want to list folders in a directory in C++, ideally in a portable (working in the major Operating Systems) way. I tried using POSIX, and it works correctly, but how can i identify whether the found item is a folder?

使用C ++ 17 std::filesystem 库:

Using the C++17 std::filesystem library:

std::vector<std::string> get_directories(const std::string& s)
{
    std::vector<std::string> r;
    for(auto& p : std::filesystem::recursive_directory_iterator(s))
        if (p.is_directory())
            r.push_back(p.path().string());
    return r;
}