boost递归遍历文件夹

最近一直处于非常忙碌的状态,一直没有时间给大家带来新的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里面提供了建立文件夹,路径的拼接,获取文件名,获取不带后缀的文件名。上面的函数都有用到了,大家自己做个测试就一切都懂了!最后来一个图(避免无图无真相):



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值