C++使用BOOST操作文件、目录

来源:互联网 发布:linux搭建dns教程 编辑:程序博客网 时间:2024/05/29 17:50

在BOOST库出现之前,C++对于文件和目录的操作,大都借助于UNIX提供的底层文件和目录接口,从使用角度来看,这些底层的操作不够友好。BOOST中filesystem库是一种可移植的文件系统操作库,可以跨平台的操作目录、文件等,在不失性能的情况下,提供了友好的操作方法。
本文主要介绍在UNIX环境中,boost::filesystem的常用操作方法。
假设你已经安装好了boost库,使用boost::filesystem需要加上头文件

#include <boost/filesystem.hpp>

编译时,需要链接

-lboost_filesystem

当安装路径不是UNIX环境变量中设置的标准路径的话,编译时还需加上boost库头文件和动态路路径,即:

-I $(BOOST)/include/-L $(BOOST)/lib/

变量$(BOOST)是BOOST库实际安装路径。

路径(path类)和迭代器–filesystem操作的基础
path类提供了路径操作的丰富接口,可以获得文件名、拓展名、文件属性等。迭代器提供了遍历整个目录所有文件的功能,常用的filesystem库的迭代器是:directory_iterator和recursive_directory_iterator,后者相对于前者提供了递归遍历的功能。基于路径和迭代器,下文已迭代目录(cur/)为例,简介filesystem的基本操作:

首先,定义要处理文件的路径
string curPath = “/home/test/cur/” ;
条件假设:
1).cur目录下结构如下
cur/
—build.sh
—src/
——main.cpp
——makefile
2).进入/home/test/cur目录,执行build.sh编译程序后,留在当前目录执行可执行文件
3).假设程序扫描目录时,首先扫描到的文件时 build.sh

//定义一个可以递归的目录迭代器,用于遍历boost::filesystem::recursive_directory_iterator itEnd;for(boost::filesystem::recursive_directory_iterator itor( curPath.c_str() ); itor != itEnd ;++itor){    //itor->string()是目录下文件的路径    /*     *当curPath是相对路径时,itor->string()也是相对路径     *即当curPath = "../cur/",下面将输出"../cur/build.sh"     */    //当curPath是绝对路径时,itor->string()也是绝对路径    string file =  itor->string() ; // "/home/test/cur/build.sh"    //构造文件路径,以获得文件丰富的操作    //path可以接受C风格字符串和string类型作为构造函数的参数,而提供的路径可以是相对路径,也可以是绝对路径。    boost::filesystem::path filePath(file);    //parent_path()获得的是当前文件的父路径    cout<<filePath.parent_path()<<endl;  // "/home/test/cur/"    //filename()获得的是文件名,含拓展名    cout<<filePath.filename()<<endl;  // "build.sh"    //stem()获得的是文件的净文件名,即不含拓展名    cout<<filePath.stem()<<endl; // "build"    //extension()文件的拓展名(主要是".sh"而不是"sh")    cout<<filePath.extension()<<endl; // ".sh"    //获得文件的大小,单位为字节    int nFileSize = boost::filesystem::file_size(filePath);    //最后一次修改文件的时间    //last_write_time()返回的是最后一次文件修改的绝对秒数    //last_write_time(filePath,time(NULL))还可以修改文件的最后修改时间,相当于Linux中命令的touch    if(filePath.last_write_time() - time(NULL) > 5)    {        /*         *在工程实践中,当需要不断的扫目录,而目录又会不断的加入新文件时,         *借助last_write_time()可以判断新入文件的完整性,以避免错误的处理还未写完的文件         */    }    //判断文件的状态信息    if(boost::filesystem::is_regular_file(file))    {        //is_regular_file(file)普通文件        //is_directory(file)目录文件,如当遍历到"/home/test/cur/src/"时,这就是一个目录文件        //is_symlink(file)链接文件        ...    }    //更改拓展名    boost::filesystem::path tmpPath = filePath;    //假设遍历到了cpp文件,想看下对应的.o文件是否存在    tmpPath.replace_extension(".o");    //判断文件是否存在    if( boost::filesystem::exists( tmpPath.string() ) )    //删除文件    //remove只能删除普通文件,而不能删除目录    boost::filesystem::remove(tmpPath.string());    //remove_all则提供了递归删除的功能,可以删除目录    boost::filesystem::remove_all(tmpPath.string());    //移动文件 & 拷贝文件    //srcPath原路径,srcPath的类型为string    //destPath目标路径,destPath的类型为string    boost::filesystem::rename(srcPath , destPath);    boost::filesystem::copy_file(srcPath , destPath);    //拷贝目录    boost::filesystem::copy_files("/home/test","/dev/shm")}boost::filesystem还可以创建目录:if( !boost::filesystem::exists( strFilePath ) ){    boost::filesystem::create_directories(strFilePath)}

boost::filesystem提供的操作当然不只如此,详见参考文件1。使用boost::filesystem操作时加上异常捕获,也能够增加代码的鲁棒性,在此不进行累述。


1.罗剑锋.BOOST程序库完全开发指南.电子工业出版社,2010.

0 0
原创粉丝点击