1.只在当前目录下遍历
#include <boost/filesystem.hpp> string targetPath="/home/test/target"; boost::filesystem::path myPath(targetPath); boost::filesystem::directory_iterator endIter; for (boost::filesystem::directory_iterator iter(myPath); iter != endIter; iter++) { if (boost::filesystem::is_directory(*iter)) { cout << "is dir" << endl; cout << iter->path().string() << endl; } else { cout << "is a file" << endl; cout << iter->path().string() << endl; } }
2.在当前目录下递归遍历
#include <boost/filesystem.hpp> string targetPath="/home/test/target"; boost::filesystem::path myPath(targetPath); boost::filesystem::recursive_directory_iterator endIter; for (boost::filesystem::recursive_directory_iterator iter(myPath); iter != endIter; iter++) { if (boost::filesystem::is_directory(*iter)) { cout << "is dir" << endl; cout << iter->path().string() << endl; } else { cout << "is a file" << endl; cout << iter->path().string() << endl; } }