c++ - 如何获取给定文件夹中具有特定扩展名的文件列表?

标签 c++ filesystems file-management

我想获取给定文件夹(以及其子文件夹)中具有特定扩展名的所有文件的文件名。也就是说,文件名(和扩展名),而不是完整的文件路径。这在 Python 等语言中非常简单,但我不熟悉 C++ 中的结构。怎么办?

最佳答案

#define BOOST_FILESYSTEM_VERSION 3
#define BOOST_FILESYSTEM_NO_DEPRECATED 
#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

/**
 * \brief   Return the filenames of all files that have the specified extension
 *          in the specified directory and all subdirectories.
 */
std::vector<fs::path> get_all(fs::path const & root, std::string const & ext)
{
    std::vector<fs::path> paths;

    if (fs::exists(root) && fs::is_directory(root))
    {
        for (auto const & entry : fs::recursive_directory_iterator(root))
        {
            if (fs::is_regular_file(entry) && entry.path().extension() == ext)
                paths.emplace_back(entry.path().filename());
        }
    }

    return paths;
}             

关于c++ - 如何获取给定文件夹中具有特定扩展名的文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11140483/

相关文章:

android - c++控制台应用程序连接到android应用程序

C++结构对象调用一个函数

c++ - 在 C/C++ 中创建新的证书存储

javascript - 检测所有顶级目录

macos - 无法删除沙箱下主包中的文件

xcode4 - 尝试添加文件,收到错误消息 : The operation couldn't be completed. 文件存在

c++ - 除非存在特定的 printf 语句,否则 fwrite 不会将缓冲区写入标准输出

Python mkdir 给我错误的权限

html - PERSISTENT 文件系统存储与 chrome 存储在哪里?

ios - 如何获取保存在临时目录中的视频的网址