C++ FileSystem 递归遍历文件夹


#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
void traverseDirectory(const std::string& path)
{
    for (const auto& entry : fs::directory_iterator(path))
    {
        if (entry.is_directory())
        {
            std::cout << "Directory: " << entry.path().string() << std::endl;
            traverseDirectory(entry.path().string());
        }
        else if (entry.is_regular_file())
        {
            std::cout << "File: " << entry.path().string() << std::endl;
            std::cout << "Name: " << entry.path().filename().string()<< std::endl;
            std::cout << "Ext:  " << entry.path().extension().string()<< std::endl;
            std::cout << "Path: " << entry.path().parent_path().string()<< std::endl;
        }
    }
}
int main()
{
    std::string directoryPath = "D:\\gis-data\\wgs84-tile";
    traverseDirectory(directoryPath);

    return 0;
}

 

posted @ 2023-11-30 16:32  经纬视界  阅读(416)  评论(0编辑  收藏  举报