boost递归遍历文件夹

来源:互联网 发布:体育产业发展数据 编辑:程序博客网 时间:2024/05/17 01:20

最近一直处于非常忙碌的状态,一直没有时间给大家带来新的boost使用的文章,好不容易抽出20分钟,这里给大家带来了一个文件夹遍历,并且更加一个源文件目录,构建一个与源文件类似的目标目录。

废话少说,先贴上代码:

#include <boost/filesystem.hpp>#include <string>void resucurePath(boost::filesystem::path src_path, boost::filesystem::path dest_path){  using namespace boost::filesystem;  if (is_directory(src_path))  {    directory_iterator tmp_directory_end;    directory_iterator tmp_dir_it(src_path);    for (tmp_dir_it; tmp_dir_it != tmp_directory_end; tmp_dir_it++)    {      path child_dest_path = dest_path;      if (is_directory(*tmp_dir_it))      {        std::string tmp_dir_name = (*tmp_dir_it).path().filename().string();        child_dest_path.append(tmp_dir_name.begin(), tmp_dir_name.end());        if (!exists(child_dest_path))        {          create_directory(child_dest_path);        }        std::cout << child_dest_path << std::endl;      }      else      {        std::string tmp_dir_name = (*tmp_dir_it).path().stem().string();        child_dest_path.append(tmp_dir_name.begin(), tmp_dir_name.end());      }      resucurePath((*tmp_dir_it).path(), child_dest_path);    }  }  else if (is_regular_file(src_path))  {    FILE *fp(NULL);    fp = fopen(dest_path.string().c_str(), "w+b");    fclose(fp);    fp = NULL;    std::cout << dest_path << std::endl;  }  }void main(){  using namespace boost::filesystem;  path src_path("E:/test/test_dir/test_src");  path dest_path("E:/test/test_dir/test_dest");  resucurePath(src_path, dest_path);}
这里的路径是我自己的测试路径,大家可以随意指定自己需要构建的路径。boost里面提供了建立文件夹,路径的拼接,获取文件名,获取不带后缀的文件名。上面的函数都有用到了,大家自己做个测试就一切都懂了!最后来一个图(避免无图无真相):
0 0
原创粉丝点击