1.boost库文档
2.boost::filesystem
- boost::filesystem库提供了两个头文件,一个是<boost/filesystem.hpp>,这个头文件包括基本的库内容。它提供了对文件系统的重要操作。同一时候它定义了一个类path。正如大家所想的。这个是一个可移植的路径表示方法,它是filesystem库的基础。
- 一个是<boost/filesystem/fstream.hpp>。是对std::fstream的一个补充,使用能够使用类boost::path作为參数。从而使得filesystem库与标准库的关系更亲热。
- 由于文件系统对于大多数系统来说都是共享的,所以不同的进程能够同一时候操作同一个对象,因此filesysetm不提供这方面的特性保证。当然这样的保证也是不可能的。或者至少昂贵的。
- filesystem在不论什么时候,仅仅要不能完毕对应的任务。它都可能抛出 basic_filesystem_error异常。当然并不是总会抛出异常。由于在库编译的时候能够关闭这个功能。同一时候有两个函数提供了无异常版本号。这是由于在任务不能完毕时并不是是异常。
- filesystem库的全部内容定义在boost名字空间的一个下级名字空间里,它叫boost::filesytem。在使用boost.filesytem之后,链接时须要加“-lboost_filesystem-mt”选项,由于这个须要额外的链接,并不是一个纯头文件的库。
3.基本功能
boost::filesystem::+下面命令
路径是否存在和是否为路基 在路径后面加'/'没有区别。
- system_complete(path); 返回完整路径(相对路径 + 当前路径)
- exists(path); 目录是否存在 文件、文件夹都可以
- is_directory(path);
- is_directory(file_status); 是否是路径 检测路径
- is_empty(path); 文件夹是否为空,必须保证路径存在,否则抛异常
- is_regular_file(path);
- is_regular_file(file_status); 是否是普通文件 普通文件,非文件夹
- is_symlink(path);
- is_symlink(file_status); 是否是一个链接文件
- file_status status(path); 返回路径名对应的状态
- initial_path(); 得到程序运行时的系统当前路径
- current_path(); 得到系统当前路径
- current_path(const Path& p); 改变当前路径
- space_info space(const Path& p); 得到指定路径下的空间信息,space_info 有capacity, free 和 available三个成员变量,分别表示容量,剩余空间和可用空间。
- last_write_time(const Path& p); 最后修改时间
- last_write_time(const Path& p, const std::time_t new_time); 修改最后修改时间
- bool create_directory(const Path& dp); 建立路径 建立文件夹
- create_hard_link(const Path1& to_p, const Path2& from_p);
- error_code create_hard_link(const Path1& to_p, const Path2& from_p, error_code& ec); 建立硬链接
- create_symlink(const Path1& to_p, const Path2& from_p);
- create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec); 建立软链接
- remove(const Path& p, system::error_code & ec = singular); 删除空文件
- remove_all(const Path& p); 删除文件以及以下的所有内容
- rename(const Path1& from_p, const Path2& to_p); 重命名
- copy_file(const Path1& from_fp, const Path2& to_fp); 拷贝文件
- omplete(const Path& p, const Path& base = initial_path<Path>()); 以base以基,p作为相对路径,返回其完整路径
- create_directories(const Path & p); 建立路径 建立文件夹
4.基本案例1
#include <boost/filesystem.hpp>
#include <string>
int main()
{
boost::filesystem::path path("/test/test1"); //初始化
boost::filesystem::path old_cpath = boost::filesystem::current_path(); //取得当前程序所在文件夹
boost::filesystem::path parent_path = old_cpath.parent_path();//取old_cpath的上一层父文件夹路径
boost::filesystem::path file_path = old_cpath / "file"; //path支持重载/运算符
if(boost::filesystem::exists(file_path)){ //推断文件存在性
std::string strPath = file_path.string();
int x = 1;
} else {
//文件夹不存在;
boost::filesystem::create_directory(file_path); //文件夹不存在。创建
}
bool bIsDirectory = boost::filesystem::is_directory(file_path); //推断file_path是否为文件夹
boost::filesystem::recursive_directory_iterator beg_iter(file_path);
boost::filesystem::recursive_directory_iterator end_iter;
for (; beg_iter != end_iter; ++beg_iter) {
if (boost::filesystem::is_directory(*beg_iter)){
continue;
}else{
std::string strPath = beg_iter->path().string(); //遍历出来的文件名称
int x=1;
}
}
boost::filesystem::path new_file_path = file_path / "test.txt";
if(boost::filesystem::is_regular_file(new_file_path)){ //推断是否为普通文件
UINT sizefile = boost::filesystem::file_size(new_file_path); //文件大小(字节)
int x =1;
}
boost::filesystem::remove(new_file_path);//删除文件new_file_path
}
5基本案例2
std::string map_path1 = "/home/megvii/1carto_work/src/map_manager/maps/test";
std::cout<<"map_path1:"<<map_path1<<std::endl;
boost::filesystem::path filePath(map_path1);
if(boost::filesystem::exists(filePath)){
std::cout<<"目录存在:"<<std::endl;
/*返回删除的文件夹和文件数,包括路径本身
* remove_all 删除文件夹及其下全部文件
* remove 删除文件或空文件夹
*/
uintmax_t test = boost::filesystem::remove_all(map_path1);
std::cout<<"删除该目录:"<<std::endl;
}else{
std::cout<<"目录不存在:"<<std::endl;
/*文件夹不存在。创建*/
boost::filesystem::create_directory(map_path1);
std::cout<<"创建该目录:"<<std::endl;
}