我目前正在使用以下代码:
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
您需要使用 astd::recursive_directory_iterator
来获取嵌套目录的路径:
for (const auto& entry : std::filesystem::recursive_directory_iterator(path)) {
paths.push_back(entry.path().string());
}
此外,您可以直接存储它们,而不是将path
s存储为string
s,如下所示:
std::vector<std::filesystem::path> paths;
string
如果需要,您可以稍后将它们转换为s,但您也可以对它们执行其他操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句