c++使用boost库遍历文件夹

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;
  }
}
原文地址:https://www.cnblogs.com/sssblog/p/9707734.html