您的位置:首页 > 其它

boost::filesystem 库(文件系统)

2016-07-14 17:44 337 查看
filesystem 库是一个可移植的文件按系统操作库。可以跨平台操作目录、文件等。

#include <boost/filesystem.hpp>
using namespace boost::filesystem;

#include <iostream>
using namespace std;

void os_test()
{
path p1("./a_dir");
cout << p1 << endl;

path p2("d:/boost/test.txt");
cout << p2 << endl;

p1 /= "etc";  //使用operator/=追加路径
cout << p1 << endl;

p1.append("test.txt");  //追加
cout << p1 << endl;

path p3("./");
p3 = system_complete("./");
cout << p3 << endl;  //返回路径在当前文件系统中的完整路径。
path p4("../");
cout << system_complete(p4) << endl;

cout << p2.string() << endl;
cout << p2.parent_path() << endl;  //父路径
cout << p2.stem() << endl;  //不含扩展名的全路径名
cout << p2.filename() << endl;  //文件名
cout << p2.extension() << endl;  //扩展名

cout << p2.root_name() << endl;
cout << p2.root_directory() << endl;
cout << p2.root_path() << endl;  //根路径

//判断路径是否有文件名
if (p3.has_filename()){
cout << "has file " << endl;
}
else{
cout << "dont has file" << endl;
}

cout << p2 << endl;
cout << p2.replace_extension("ldd") << endl;  //变更文件扩展名
//cout<<p2.remove_filename() << endl;  //删除路径中最后的文件名

//文件异常
try{
cout<<file_size(p2);
}
catch (filesystem_error& e){
cout << e.what() << endl;
cout << e.path1() << endl;
}

//
path p5 = initial_path();  //exe所在路径
cout << p5 << endl;
cout << current_path() << endl;  //当前文件路径
try{
cout << file_size(p5) << endl;
}
catch (filesystem_error& e){
cout << e.what() << endl;
cout << e.path1() << endl;
}

int GB = 1024 * 1024 * 1024;
space_info si = space("d:/");  //磁盘空间分配情况
cout << si.capacity/GB << ends << si.available/GB << ends << si.free/GB << endl;

//使用path对象打开一个文本文件,并打印到标准输出上。
path p6("d:/filesystem_test.txt");
ifstream ifs(p6.string());
cout << ifs.rdbuf();

}


后面还有:

迭代目录功能

查找文件··

拷贝目录··
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  boost