如何在目录及其子文件夹中构建文件名字符串向量?
How to build a vector of strings of filenames in a directory and its subfolders?
我目前正在使用以下代码:
std::vector<std::string> paths;
std::string path = "Assets/";
for (const auto& entry : std::filesystem::directory_iterator(path)) {
paths.push_back(entry.path().string());
}
但是,它会忽略Assets/
文件夹中的子文件夹。如何让它跟踪子文件夹中的所有文件?
例如。假设Assets/
目录如下所示:
Assets/
---image01.png
---image02.png
---somefile.txt
---subfolder/
------example.png
---anotherfolder/
------anotherfile.txt
我希望矢量看起来像这样:
Assets/image01.png
Assets/image02.png
Assets/somefile.txt
Assets/subfolder/example.png
Assets/anotherfolder/anotherfile.txt
您需要使用std::recursive_directory_iterator
来获取嵌套目录的路径:
for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
paths.push_back(entry.path().string());
}
此外,您可以直接存储它们,而不是将path
存储为string
s,如下所示:
std::vector<std::filesystem::path> paths;
如果需要,您可以稍后将它们转换为string
,但您也可以对它们执行其他操作。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 无法在 CLion 中构建 C++ 项目
- 从命令行c++发送文本文件名
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用ndk-build.cmd构建Android.so文件
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- libssh 的函数在构建 libssh 时无法在 Qt 和 cmake 错误中找到
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 无法使用Qt Creator在Windows中构建yaml-cpp
- 构建一个由C和C++文件组成的库
- llvm构建器向基本块添加终止符
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 如何跨平台将二进制资源构建到程序中?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 如何解决 Ninja c++ 构建和执行问题
- 如何在目录及其子文件夹中构建文件名字符串向量?