我想编写一个C++程序,该程序从目录中读取多个文件,文件的数量不确定。我知道一个Python实现-OS.walk,可以完美地完成此工作:
Python OS.walk
有人对OS.walk功能的C++实现有任何想法吗?
提前致谢
最佳答案
#include <boost/filesystem.hpp>
#include <iostream>
int main()
{
boost::filesystem::path path = boost::filesystem::current_path();
boost::filesystem::recursive_directory_iterator itr(path);
while (itr != boost::filesystem::recursive_directory_iterator())
{
std::cout << itr->path().string() << std::endl;
++itr;
}
}
直接取自http://www.deanwarrenuk.com/2012/09/how-to-recursively-walk-folder-in-c.html
这很好地解释了为什么您需要boost库来隐藏文件系统中的差异。