【问题标题】:How can I list the file names from a folder for a specific extension in C++? [duplicate]如何在 C++ 中列出特定扩展名的文件夹中的文件名? [复制]
【发布时间】:2015-02-28 07:09:19
【问题描述】:

例如,我想从 c:/user/documents 文件夹中获取所有 .pdf 文件

【问题讨论】:

    标签: c++ file directory


    【解决方案1】:

    How to get list of files with a specific extension in a given folder 的副本

    #define BOOST_FILESYSTEM_VERSION 3
    #define BOOST_FILESYSTEM_NO_DEPRECATED 
    #include <boost/filesystem.hpp>
    
    namespace fs = ::boost::filesystem;
    
    // return the filenames of all files that have the specified extension
    // in the specified directory and all subdirectories
    void get_all(const fs::path& root, const string& ext, vector<fs::path>& ret)
    {
        if(!fs::exists(root) || !fs::is_directory(root)) return;
    
        fs::recursive_directory_iterator it(root);
        fs::recursive_directory_iterator endit;
    
        while(it != endit)
        {
            if(fs::is_regular_file(*it) && it->path().extension() == ext) ret.push_back(it->path().filename());
            ++it;
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 2019-11-03
      • 2011-09-25
      • 1970-01-01
      • 2019-03-02
      • 2011-03-10
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      相关资源
      最近更新 更多