Newspeak0x

C++ 17 filesystem directory_entry to string directory_entry 转换成 string

http://www.cpp.re/forum/beginner/263980/

 

#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <fstream>

// return a list of names of regular files in the directory 
std::vector<std::string> file_name_list( const std::string& path_to_dir )
{
    namespace fs = std::filesystem ;

    if( fs::is_directory(path_to_dir) )
    {
        std::vector<std::string> file_names ;

        for( const auto& entry : fs::directory_iterator(path_to_dir) )
            if( entry.is_regular_file() ) file_names.push_back( fs::absolute( entry.path() ).string() ) ;

        return file_names ;
    }

    else return {} ;  // not a directory; return empty vector
}

int main()
{
    const std::string directory = "C:\\Logs" ;

    for( const std::string& fname : file_name_list(directory) ) std::cout << fname << '\n' ;
}

posted on 2022-05-03 15:25  Newspeak0x  阅读(110)  评论(0)    收藏  举报

导航