Boost.Filesystem 库为对路径、文件和目录进行查询和操作提供了可移植的工具,已经被C++标准委员会接纳包含到TR2中。

编译

使用Boost.Filesystem 库之前要先编译它,请参考《Boost的编译》

头文件

#include <boost/filesystem.hpp>

所有Boost.Filesystem库的内容都处于名空间boost::filesystem之内。

认识basic_path类

在Boost.Filesystem库里basic_path是最重要的类,它以系统无关的方式保存路径、文件名。象std::basic_string 一样,针对char和wchar_t,分别特化了path和wpath。

basic_path的构造函数:

basic_path( const string_type & s ); 
basic_path( const value_type * s );
template <class InputIterator> basic_path(InputIterator s, InputIterator last);

输入参数是一个字符串(或字符迭代器),表示路径名,可以输入系统原生路径名或可移植路径名
原生路径名没啥好说的,比如C:\Windows; D:\abc\ttt.txt等
可移植路径名的定义和Unix的路径定义相同,以“/”作为路径分隔符。

basic_path成员函数:

成员函数 作用
template <class InputIterator>basic_path& append(InputIterator first, InputIterator last); 将字符串 s 或字符序列 [first,last) 中的路径元素追加到保存的路径中。
basic_path& remove_filename(); 去除路径中的文件名
basic_path& replace_extension( const string_type & new_extension = "" ); 替换扩展名
string_type string() 得到可移植路径名
string_type file_string() 得到系统原生文件名
string_type directory_string() 得到系统原生路径名
string_type root_name() const; 得到根名
string_type root_directory() const; 得到根目录
basic_path root_path() const; 得到根路径:根名+根目录
basic_path relative_path() const; 得到相对路径
string_type filename() const; 得到文件名
basic_path parent_path() const; 得到父路径:根路径+相对路径
string_type stem(const Path & p) const; 得到不带扩展名的文件名
string_type extension(const Path & p) const; 得到扩展名
bool empty() const; path未赋值
bool is_complete() const; 是否是完整路径
bool has_root_path() const;
bool has_root_name() const;
bool has_root_directory() const;
bool has_relative_path() const;
bool has_filename() const;
bool has_branch_path() const;
路经中是否包含指定的项

测试代码:

  1. #include "boost/filesystem.hpp"   // 包含所有需要的 Boost.Filesystem 声明
  2. #include <iostream>               // 使用 std::cout
  3. namespace fs = boost::filesystem;
  4. // 宏FSTEST:测试f的成员函数,输出成员函数名和结果
  5. #define FSTEST(x) std::cout << #x##": " << f.x << std::endl
  6. int main()
  7. {
  8. fs::path f("\\folder1\\folder2\\folder3\\filename.ext");
  9. FSTEST(string());
  10. FSTEST(file_string());
  11. FSTEST(directory_string());
  12. FSTEST(root_name());
  13. FSTEST(root_directory());
  14. FSTEST(root_path());
  15. FSTEST(relative_path());
  16. FSTEST(filename());
  17. FSTEST(parent_path());
  18. FSTEST(stem());
  19. FSTEST(extension());
  20. FSTEST(replace_extension("new"));
  21. char buf[]="hello";
  22. FSTEST(append(buf, buf+sizeof(buf)));
  23. FSTEST(remove_filename());
  24. return 0;
  25. }

输出:

string(): /folder1/folder2/folder3/filename.ext 
file_string(): \folder1\folder2\folder3\filename.ext
directory_string(): \folder1\folder2\folder3\filename.ext
root_name():
root_directory(): /
root_path(): /
relative_path(): folder1/folder2/folder3/filename.ext
filename(): filename.ext
parent_path(): /folder1/folder2/folder3
stem(): filename
extension(): .ext
replace_extension("new"): /folder1/folder2/folder3/filename.new
append(buf, buf+sizeof(buf)): /folder1/folder2/folder3/filename.new/hello
remove_filename(): /folder1/folder2/folder3/filename.new/

常用函数

函数名 作用
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); 返回路径名对应的状态
template <class Path> const Path& initial_path(); 得到程序运行时的系统当前路径
template <class Path> Path current_path(); 得到系统当前路径
template <class Path> void current_path(const Path& p); 改变当前路径
template <class Path> space_info space(const Path& p); 得到指定路径下的空间信息,space_info 有capacity, free 和 available三个成员变量,分别表示容量,剩余空间和可用空间。
template <class Path> std::time_t last_write_time(const Path& p); 最后修改时间
template <class Path> void last_write_time(const Path& p, const std::time_t new_time); 修改最后修改时间
template <class Path> bool create_directory(const Path& dp); 建立路径
template <class Path1, class Path2> void create_hard_link(const Path1& to_p, const Path2& from_p);
template <class Path1, class Path2> error_code create_hard_link(const Path1& to_p, 
const Path2& from_p, error_code& ec);
建立硬链接
template <class Path1, class Path2> void create_symlink(const Path1& to_p, const Path2& from_p);
template <class Path1, class Path2> error_code create_symlink(const Path1& to_p, const Path2& from_p, error_code& ec); 
建立软链接
template <class Path> void remove(const Path& p, system::error_code & ec = singular ); 删除文件
template <class Path> unsigned long remove_all(const Path& p); 递归删除p中所有内容,返回删除文件的数量
template <class Path1, class Path2> void rename(const Path1& from_p, const Path2& to_p); 重命名
template <class Path1, class Path2> void copy_file(const Path1& from_fp, const Path2& to_fp); 拷贝文件
template <class Path> Path complete(const Path& p, const Path& base=initial_path<Path>()); 以base以基,p作为相对路径,返回其完整路径
template <class Path> bool create_directories(const Path & p); 建立路径

路径迭代器

basic_directory_iterator

构造函数:

explicit basic_directory_iterator(const Path& dp); 
basic_directory_iterator();

basic_directory_iterator 从构造参数得到目录,每一次调用 operator++,它就查找并得到下一个文件名直到目录元素的末尾。不带参数的构造函数 basic_directory_iterator() 总是构造一个 end 迭代器对象,它是唯一一个用于结束条件的合法迭代器。

示例代码,得到指定目录下的所有文件名:

  1. void find_file( const fs::path & dir_path )
  2. {
  3. if ( !fs::exists( dir_path ) ) return;
  4. fs::directory_iterator end_itr; // 缺省构造生成一个结束迭代器
  5. for ( fs::directory_iterator itr( dir_path );
  6. itr != end_itr;
  7. ++itr )
  8. {
  9. if ( fs::is_directory(itr->status()) )
  10. {
  11. find_file( itr->path() ); //递归查找
  12. }
  13. else
  14. {
  15. std::cout << *itr << std::endl;
  16. }
  17. }
  18. }

basic_recursive_directory_iterator

递归遍历目录的迭代器,它的构造参数与basic_directory_iterator相同,当调用 operator++时,如果当前值是一个目录,则进入下一级目录。
它有三个成员函数:

函数名 作用
int level() const; 得到当前搜索深度
void pop(); 调用pop()后,下一次递增就会直接返回上一级目录
void no_push(); 调用no_push()后,即便下一个元素是目录类型也不进入

示例代码,得到指定目录下的所有文件名(和上例作用相同):

  1. void find_file2( const fs::path & dir_path )
  2. {
  3. fs::recursive_directory_iterator end_itr; // 缺省构造生成一个结束迭代器
  4. for ( fs::recursive_directory_iterator itr( dir_path );
  5. itr != end_itr;
  6. ++itr )
  7. {
  8. std::cout << itr.level() << *itr << std::endl;
  9. }
  10. }

Boost学习之可移植路径操作--filesystem的更多相关文章

  1. Windows路径操作API函数学习

    前言 在VC++开发过程中,经常需要用到一些路径操作,比如拼需要的文件路径,搜索路径中的内容等等.Windows提供了一套关于路径操作的API帮助我们更好的执行这些操作. 路径截断与合并API Pat ...

  2. Windows路径操作API函数学习【转载】

    文章出自https://www.cnblogs.com/MakeView660/p/6644838.html 前言 在VC++开发过程中,经常需要用到一些路径操作,比如拼需要的文件路径,搜索路径中的内 ...

  3. BOOST学习笔记

    BOOST学习笔记 1 tool #pragma once #include <vector> #include "boost/noncopyable.hpp" #in ...

  4. Boost学习总结(一)VS2010环境下编译STLport和Boost

    Boost学习总结(一)VS2010环境下编译STLport和Boost Boost简介 Boost库是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库.1998年,Beman G.Da ...

  5. tensorflow学习笔记——使用TensorFlow操作MNIST数据(2)

    tensorflow学习笔记——使用TensorFlow操作MNIST数据(1) 一:神经网络知识点整理 1.1,多层:使用多层权重,例如多层全连接方式 以下定义了三个隐藏层的全连接方式的神经网络样例 ...

  6. Xamarin Essentials教程获取路径文件系统FileSystem

    Xamarin Essentials教程获取路径文件系统FileSystem 文件系统用于管理设备内的各类文件.通过文件系统,应用程序可以创建永久文件和临时文件,也可以获取预先打包的文件,如预设数据库 ...

  7. boost 学习笔记 0: 安装环境

    boost 学习笔记 0: 安装环境 最完整的教程 http://einverne.github.io/post/2015/12/boost-learning-note-0.html Linux 自动 ...

  8. Emacs学习心得之 基础操作

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Emacs学习心得之 基础操作 1.前言与学习计划2.Emacs基础操作 一. 前言与学习计 ...

  9. 【代码笔记】iOS-缓存路径操作类

    一,代码. AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplica ...

随机推荐

  1. if语句判断闰年、平年

     一.让用户输入一个年份,判断是否是闰年. 判断一个年份是否是闰年有两个条件 ①能被400整除:②能被4整除但是不能被100整除 Console.WriteLine("请输入年份:" ...

  2. BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级( 最短路 )

    最短路...多加一维表示更新了多少条路 -------------------------------------------------------------------------------- ...

  3. 使用wget -i下载多个文件

    使用wget -i下载多个文件 命令: wget -i filelist.txt 说明: 首先,保存一份下载链接文件 cat > filelist.txt url1 url2 url3 url4

  4. shell学习之变量、判断、重复动作

    1.1 环境以及变量的定义.赋值.展开.删除 export:将一个变量导入到环境中:export PATH=$PATH:/home. readonly 讲一个变量设置为只读模式,在shell脚本中定义 ...

  5. 【Eclipse】修改java代码不强制重启

    找到tomcat的server.xml文件,修改以下代码,重新发布重启.然后修改java代码就可以不用重启了. 将reloadable=“true”改成reloadable="false&q ...

  6. ViewPager+Fragment实现支持左右滑动的Tab

    主要思想:顶部标题栏top.xml,中间ViewPager(4个Fragment),底部导航 top.xml和bottom.xml在我之前的两个随笔里有,此处不再赘述. activity_main.x ...

  7. tomcat解析之简单web服务器(图)

    链接地址:http://gogole.iteye.com/blog/587163 之前有javaeyer推荐了一本书<how tomcat works>,今天晚上看了看,确实不错,第一眼就 ...

  8. SGU 183. Painting the balls( dp )

    dp..dp(i, j)表示画两个点为i-j, i的最优答案. dp(i, j) = min{ dp(i-j, k) } + cost[i] (1≤k≤M-j) 令f(i, j) = min{dp(i ...

  9. ZOJ 3329 One Person Game 【概率DP,求期望】

    题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0 设dp[i]表示达到 ...

  10. BNU 4067 求圆并

    好久没写过单组数据的题目了 QAQ 赤裸裸的模板题 #include <cstdio> #include <cstring> #include <iostream> ...