boost filesystem测试

21 篇文章 2 订阅
/*
Title:boost filesystem 子库测试
Author:kagula
Date:2016-01-18
Environment:VS2013Update5 boost 1.57
Prologue:
为了使今后的代码更有可移植性测试下boost filesystem。
Reference:
[1]《(九)boost库之文件处理filesystem》
http://my.oschina.net/lingluonianhua/blog/213999
[2]http://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/index.htm
*/

#include <boost/filesystem.hpp>


#include <iostream>
using namespace std;

void TestCreate()
{
	//若目录已经存在,也不会抛出异常。
	boost::filesystem::create_directories("d:/temp");
}

void TestDelete()
{
	try
	{
		//删除文件
		{
			boost::filesystem::path filename;
			filename = "D:/temp/Launcher.exe";
			remove(filename);//如果filename变量存放的是dir,会抛出异常.
		}

		//删除目录(非空folder也会被删除)
		{
			boost::filesystem::path dir;
			dir = "D:/temp";
			remove_all(dir);
		}
	}
	catch (boost::filesystem::filesystem_error& /*e*/)
	{
	}
}

void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst);
void TestModify()
{
	try
	{
		//copy_directory老是失败,原来它是create directory功能,方法名造成了误导!
		//真实的意思是创建tempDest目录,这个目录的属性来自tempSrc目录。
		//boost::filesystem::copy_directory("D:/tempSrc/", "d:/tempDest");

		//下面的代码调用失败会throw exception.
		//boost::filesystem::rename("d:/temp", "d:/temp2");

		//"D:/apache-activemq-5.13.0"目录下的文件复制到“d:/temp2”目录下.
		CopyFiles("D:/apache-activemq-5.13.0", "d:/temp2");
	}
	catch (boost::filesystem::filesystem_error& e)
	{		
		std::cout << e.path1() << std::endl;
		std::cout << e.what() << std::endl;
	}
}

void TestQuery()
{
	try
	{
		boost::filesystem::path dir("c:\\Windows\\System32");
		boost::filesystem::path file("D:/temp/Launcher.exe");

		//是文件吗?
		cout << dir.string() << "=>is_regular_file=>" << (boost::filesystem::is_regular_file(dir) ? "true" : "false") << endl;
		cout << file.string() << "=>is_regular_file=>" << (boost::filesystem::is_regular_file(file) ? "true" : "false") << endl;

		//是目录吗
		cout << dir.string() << "=>is_directory=>" << (boost::filesystem::is_directory(dir) ? "true" : "false") << endl;
		cout << file.string() << "=>is_directory=>" << (boost::filesystem::is_directory(file) ? "true" : "false") << endl;

		//文件是否存在,目录是否存在
		cout << dir.string() << "=>exists=>" << (boost::filesystem::exists(dir) ? "true" : "false") << endl;
		cout << file.string() << "=>exists=>" << (boost::filesystem::exists(file) ? "true" : "false") << endl;

		cout << "枚举(本层)目录中的所有文件" << endl;
		{
			boost::filesystem::path dir2("c:\\Windows\\System32");
			boost::filesystem::directory_iterator end;
			for (boost::filesystem::directory_iterator pos(dir2); pos != end; pos++)
			{
				std::cout << *pos << std::endl;
			}
		}

		cout << "枚举目录中的所有文件" << endl;
		{
			typedef boost::filesystem::recursive_directory_iterator rd_iterator;
			boost::filesystem::path dir2("D://apache-activemq-5.13.0");
			rd_iterator end;
			for (rd_iterator pos(dir2); pos != end; pos++)
			{
				//如果深度大于4层,则不再继续深入
				if (is_directory(*pos) && pos.level() > 4)
				{
					pos.no_push();
				}
				//如果该目录下有nofind.txt文件,则跳出该目录
				if (*pos == "nofind.txt")
				{
					pos.pop();
				}

				std::cout << *pos << std::endl;
			}
		}
	}
	catch (boost::filesystem::filesystem_error& e)
	{
		std::cout << e.path1() << std::endl;
		std::cout << e.what() << std::endl;
	}

	cout << "测试路径信息" << endl;
	{
		boost::filesystem::path dir("C:\\Windows");
		dir /= "System32";       //追加下级目录
		dir /= "services.exe";
		std::cout << dir << std::endl;
		std::cout << dir.string() << std::endl;             //转换成std::string 类型
		std::cout << dir.root_name() << std::endl;          //盘符名:C:
		std::cout << dir.root_directory() << std::endl;     //根目录:"\"
		std::cout << dir.root_path() << std::endl;          //根路径:"C:\"
		std::cout << dir.relative_path() << std::endl;      // 相对路径:Windows\System32\services.exe
		std::cout << dir.parent_path() << std::endl;        //上级目录:C:\Windows\System32
		std::cout << dir.filename() << std::endl;           //文件名:services.exe
		std::cout << dir.stem() << std::endl;               //不带扩展的文件名:services
		std::cout << dir.extension() << std::endl;          //扩展名:.exe
	}
}

// recursively copy file or directory from $src to $dst
void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst)
{
	if (!boost::filesystem::exists(dst))
	{
		boost::filesystem::create_directories(dst);
	}
	for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it)
	{
		const boost::filesystem::path newSrc = it->path();
		const boost::filesystem::path newDst = dst / it->path().filename().string();
		if (boost::filesystem::is_directory(newSrc))
		{
			CopyFiles(newSrc, newDst);
		}
		else if (boost::filesystem::is_regular_file(newSrc))
		{
			boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
		}
		else
		{
			cerr << "Error: unrecognized file - " << newSrc.string() << endl;
		}
	}
}

int main(int argc, char *argv[])
{
	cout << "boost 1.57" << endl;

	//TestCreate();
	TestModify();
	//TestDelete();
	//TestQuery();

	cin.get();

	return 0;
}


附对宏的很有意思的调用

#include "boost/filesystem.hpp"   // 包含所有需要的 Boost.Filesystem 声明
#include <iostream>               // 使用 std::cout

namespace fs = boost::filesystem;

// 宏FSTEST:测试f的成员函数,输出成员函数名和结果
#define FSTEST(x) std::cout << #x##": " << f.x << std::endl

int main(int argc, char **argv)
{
	fs::path f("\\folder1\\folder2\\folder3\\filename.ext");

	FSTEST(string());
	FSTEST(root_name());
	FSTEST(root_directory());
	FSTEST(root_path());
	FSTEST(relative_path());
	FSTEST(filename());
	FSTEST(parent_path());
	FSTEST(stem());
	FSTEST(extension());

	FSTEST(replace_extension("new"));
	char buf[] = "hello";
	FSTEST(append(buf, buf + sizeof(buf)));
	FSTEST(remove_filename());

	//
#define  FSTEST2(x) std::cout << #x##": " << fs::x << std::endl
	FSTEST2(initial_path());//得到程序运行时的系统当前路径
	FSTEST2(current_path());//得到系统当前路径

	//要得到EXE文件所在的路径或AppData路径,只能通过调用Win API实现。

	return 0;
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值