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;
  }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM