#include #include #include #include #include

using namespace std; namespace fs = std::filesystem;

// 递归函数,获取指定目录下的所有文件的绝对路径 void getFilePaths(const fs::path& dirPath, vectorfs::path& filePaths) { for (const auto& entry : fs::directory_iterator(dirPath)) { if (entry.is_regular_file()) { filePaths.push_back(entry.path().absolute()); } else if (entry.is_directory()) { getFilePaths(entry.path(), filePaths); } } }

int main() { fs::path dirPath('C:/Users/username/Desktop/test'); vectorfs::path filePaths; getFilePaths(dirPath, filePaths); for (const auto& filePath : filePaths) { cout << filePath << endl; } return 0; }

在上面的代码中,getFilePaths 函数使用了 std::filesystem 库中的 directory_iterator 和 is_regular_file 函数来遍历目录下的所有文件和子文件夹,并将文件的绝对路径存储在 vector 中。最终,在 main 函数中输出所有文件的绝对路径。

C++ 获取目录下所有文件绝对路径(递归遍历)

原文地址: https://www.cveoy.top/t/topic/n2zO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录