C++ 筆記 boost 列出資料夾底下 txt filepath

#include "boost/filesystem.hpp"
{
    using fs = boost::filesystem;
    auto p = "$THE_PATH";
    
    // 像 std::cin 的 iterator 一樣,空的代表是 end_iterator
    std::vector<fs::directory_entry> entries(fs::directory_iterator{p}, fs::directory_iterator{});
    for(const auto& entry: entries)
    {
        if (! fs::is_regular_file(entry) ) { continue; }  // discard non regular file
        if (! entry.path().extension() == ".txt") ) { continue; } // discard non txt file
        std::cout << entry.path().string() << std::endl; // print what I want
    }
}

Reference:

comments powered by Disqus