(九)boost库之文件处理filesystem

 

filesystem库是一个可移植的文件系统操作库,它在底层做了大量的工作,使用POSIX标准表示文件系统的路径,使C++具有了类似脚本语言的功能,可以跨平台操作目录、文件,写出通用的脚本程序。

1.path的构造函数可以接受C字符串和string,也可以是一个指定首末迭代器字符串序列区间。

2.filesystem提供了一系列的文件名(或目录)检查函数。

3.有丰富的函数用于获取文件名、目录名、判断文件属性等等。

4.filesystem库使用异常来处理文件操作时发生的错误。

5.filesystem库提供一个文件状态类file_status及一组相关函数,用于检查文件的各种属性,如是否存在、是否是目录、是否是符号链接等。

6.filesystem提供了少量的文件属性操作,如windows下的只读、归档等,Linux下的读写权限等。

7.文件操作,如创建目录、文件改名、文件删除、文件拷贝等等。

8.basic_directory_iterator提供了迭代一个目录下所有文件的功能。

一、path类的基本用法

   //注意 /= 和 += 的区别, /= 表示追加下级目录, +=  仅仅是字符串的串接
    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

二、常用函数及异常处理

函数名                                           作用

system_complete(path);           返回完整路径(相对路径+当前路径) 
exists(path);                                目录是否存在 
is_directory(path); 
is_directory(file_status);            是否是路径 
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);                                                                   递归删除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);                                                       建立路径

   //常用函数及异常
    try
    {
        path dir2("c:\\Windows\\System32");
        assert(is_directory(dir2));          //判断是否一个目录
        assert(exists(dir2));                //判断目录是否存在
    }
    catch(filesystem_error& e)
    {
        std::cout << e.path1() << std::endl;
        std::cout << e.what() << std::endl;
    }

三、目录迭代

boost库提供了两个迭代器

directory_iterator     :只支持本层目录遍历

recursive_directory_iterator :支持深度遍历

输出指定目录下的文件:

    path dir2("c:\\Windows\\System32");
    directory_iterator end;
    for (directory_iterator pos(dir2); pos != end; pos++)
    {
        std::cout << *pos << std::endl;
    }

使用深度遍历

    typedef recursive_directory_iterator rd_iterator;
    path dir2("E:\\Student");
    rd_iterator end;
    for (rd_iterator pos(dir); pos != end; pos++)
    {
        //如果深度大于4层,则不再继续深入
        if (is_directory(*pos) && pos.level() > 4)
        {
            pos.no_push();
        }
        //如果该目录下有nofind.txt文件,则跳出该目录
        if (*pos == "nofind.txt")
        {
            pos.pop();
        }
    }

四、示例

找出指定目录及子目录下匹配的文件

#include <iostream>
#include <boost/filesystem.hpp>
#include <boost/xpressive/xpressive_dynamic.hpp>
#include <boost/algorithm/string.hpp>
#include <vector>

using namespace boost::filesystem;
using namespace boost::xpressive;

typedef recursive_directory_iterator rd_iterator;

//获取过滤的文件
void GetFilterFile(const std::string& filter,std::vector<path>& vecFile)
{
    path filterPath =  dir / filter;
    path filterDir1 = filterPath.parent_path();
    std::string filename = filterPath.filename().string();

    //转换文件通配符为正则表达式
    std::string str = boost::replace_all_copy(boost::replace_all_copy(filename, ".", "\\."), "*", ".*");
    sregex reg = sregex::compile(str);

    if (!exists(filterDir1) || !is_directory(filterDir1))
    {
        return;
    }
    //查找当前目录及子目录的所有文件,如果符合正则表达式则添加到列表中
    rd_iterator end;
    for (rd_iterator pos(filterDir1); pos != end; pos++)
    {
        if (!is_directory(*pos) && regex_match(pos->path().filename().string(), reg))
        {
            vecFile.push_back(pos->path());
        }
    }
}

(九)boost库之文件处理filesystem的更多相关文章

  1. 使用boost库获取文件夹下所有文件名字

    最近整理项目发现一个曾经找了好久的有用的代码片段,就是获取文件夹下所有文件的名字,和当前文件的绝对路径. 记录一下. 使用的是boost库, #include <boost/filesystem ...

  2. boost库生成文件命名和编译

    生成文件命名规则:boost中有许多库,有的库需要编译.而有的库不需要编译,只需包含头文件就可以使用.编译生成的文件名字普遍较长,同一个库根据编译链接选项不同,又可以生成多个不同名字的文件.生成的文件 ...

  3. c++使用boost库遍历文件夹

    1.只在当前目录下遍历 #include <boost/filesystem.hpp> string targetPath="/home/test/target"; b ...

  4. windows下编译和安装boost库

    boost是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库. 获取方式 boost提供源码形式的安装包,可以从boost官方网站下载,目前最新版本是1.59.0. 本机上正好有boos ...

  5. vs配置boost库

    步骤: 1.在boost官网下载boost版本,以1.59.0为例. 2.解压,解压后可看到文件夹下有个bootstrap.bat文件. 注意: 如果有以下error: 'cl' 不是内部或外部命令, ...

  6. VS2013中安装配置和使用Boost库

    源地址:http://www.itnose.net/detail/6077953.html 时间:2014.07.24 地点:基地 ---------------------------------- ...

  7. Qt中使用Boost库

    关于boost库的编译,请看https://www.cnblogs.com/HackerArt/p/10539516.html 网上可以查到很多介绍qt使用库文件的教程,但是大多都没有注意到,qt中支 ...

  8. Boost库编译后命名方式

    Boost官网的<Geting Started On Windows>(http://www.boost.org/doc/libs/1_38_0/more/getting_started/ ...

  9. 新手,Visual Studio 2015 配置Boost库,如何编译和选择,遇到无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib“的解决办法

    1,到官网下载最新的boost,www.boost.org 这里我下载的1-63版本. 2,安装,解压后运行bootstrap.bat文件.稍等一小会就OK. 3,编译boost库.注意一定要使用VS ...

随机推荐

  1. HTTP -> Asp.net (第一篇)

    当用户在浏览器输入一个URL地址后,浏览器会发送一个请求到服务器.这时候在服务器上第一个负责处理请求的是IIS.然后IIS再根据请求的URL扩展名将请求分发给不同的ISAPI处理. 流程如下: 1.I ...

  2. 什么是队列(Queue)?

    类似于链表和堆栈,队列也是存储数据的结构.队列中数据进入队列的顺序很重要,一般来说,队列就是一群人或者事物按照排好的顺序等待接受服务或者处理. 定义:队列,又称为伫列(queue),是先进先出(FIF ...

  3. 开源安卓播放器:Dolphin Player 简单分析

    Dolphin播放器(Dolphin Player)是一款开源的音频和视频播放器,它支持大多数的音频和视频文件模式,也支持大部分的字幕文件格式.它是基于ffmpeg的. 项目主页:http://cod ...

  4. javadoc 生成帮助文档时,注意以下几点

    参考:http://www.w3school.com.cn/tags/tag_pre.asp javadoc 生成帮助文档时,注意以下几点: 1.函数功能描述的结尾一定要有句号,英文句号或中文句号均可 ...

  5. sql语句收集

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  6. Linux 文件操作——系统调用和标准I/O库

    一.什么是文件 在讲述文件操作之前,我们首先要知道什么是文件.看到这个问题你可能会感觉到可笑,因为对于用过计算机的人来说,文件是最简单不过的概念了,例如一个文本是一个文件,一个work文档是一个文件等 ...

  7. Linux转发性能评估与优化-转发瓶颈分析与解决方式(补遗)

    补遗 关于网络接收的软中断负载均衡,已经有了成熟的方案,可是该方案并不特别适合数据包转发,它对server的小包处理非常好.这就是RPS.我针对RPS做了一个patch.提升了其转发效率. 下面是我转 ...

  8. CSS清除浮动的方法

    CSS清除浮动的方法有哪些呢?经常性地会使用到float,很多邪门的事儿都有可能是浮动在作怪,清除浮动是必须要做的,而且随时性地对父级元素清除浮动的做法也被认为是书写CSS的良好习惯之一.下面看今天的 ...

  9. php常用的操作

    一.  php配置 1.禁止一些函数disable_functions = eval,assert,popen,passthru,escapeshellarg,escapeshellcmd,passt ...

  10. WebService应用一例,带有安全验证

    1.创建WEB项目,添加WEB服务WebService1.asmx,代码如下: using System; using System.Collections.Generic; using System ...