功能:
c++ 获取指定文件夹下的所有文件夹和子文件夹路径,并打印出来
使用vs2017 c++17标准
注意:最低标准为c++17
代码:
#include <iostream>
#include <filesystem>
#include <vector>
namespace fs = std::filesystem;
std::vector<fs::path> GetAllFolders(const fs::path& folderPath)
{
std::vector<fs::path> folderPaths;
if (!fs::exists(folderPath) || !fs::is_directory(folderPath))
{
std::cout << "Invalid folder path!" << std::endl;
return folderPaths;
}
for (const auto& entry : fs::directory_iterator(folderPath))
{
if (fs::is_directory(entry))
{
folderPaths.push_back(entry.path());
const auto subFolderPaths = GetAllFolders(entry.path());
folderPaths.insert(folderPaths.end(), subFolderPaths.begin(), subFolderPaths.end());
}
}
return folderPaths;
}
int main()
{
fs::path folderPath = "x64"; // 替换为你的文件夹路径
const auto folderPaths = GetAllFolders(folderPath);
for (const auto& path : folderPaths)
{
std::cout << "Folder: " << path.string() << std::endl;
}
return 0;
}
输出为
Folder: x64\Debug
Folder: x64\Debug\ConsoleA.0026FD2F.tlog
开启vs 17标准方法:
Visual Studio 2017默认情况下不完全支持C++17标准,但你可以通过更新编译器和工具集来启用对C++17的部分支持。
以下是在Visual Studio 2017中启用C++17支持的一些步骤:
-
确保你的Visual Studio 2017安装有最新的更新,以便获取C++17支持的改进。打开Visual Studio Installer并检查更新选项。
-
在项目属性中,设置"C/C++" -> “语言” -> “C++语言标准"选项为"ISO C++17 标准(std::c++17)”。
-
确保项目属性中的"C/C++" -> “代码生成” -> "运行时库"选项设置为"多线程 (/MT)“或"多线程调试 (/MTd)”,以避免与标准库链接的冲突。
请注意,尽管Visual Studio 2017支持部分C++17特性,但某些较新的C++17功能可能仍不可用。如果你需要更完整的C++17支持,建议考虑使用更新版本的Visual Studio,如Visual Studio 2019或Visual Studio 2022,这些版本提供了更广泛的C++17和C++20支持。
请注意在使用std::filesystem时,你可能需要包含头文件而不是<experimental/filesystem>。C++17引入了对std::filesystem的正式支持,并将其放置在头文件中。
如果在Visual Studio 2017中仍然遇到问题,可能需要考虑升级到较新的版本或者使用其他支持C++17的编译器。
本文 参考chatcpt