Boost.Filesystem 库为对路径、文件和目录进行查询和操做提供了可移植的工具,已经被C++标准委员会接纳包含到TR2中。ios
使用Boost.Filesystem 库以前要先编译它,请参考《Boost的编译》app
#include <boost/filesystem.hpp>
全部Boost.Filesystem库的内容都处于名空间boost::filesystem以内。函数
在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的路径定义相同,以“/”做为路径分隔符。测试
成员函数 | 做用 |
---|---|
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; |
路经中是否包含指定的项 |
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); | 创建路径 |
构造函数:
spa
explicit basic_directory_iterator(const Path& dp);
basic_directory_iterator();
basic_directory_iterator
从构造参数获得目录,每一次调用 operator++
,它就查找并获得下一个文件名直到目录元素的末尾。不带参数的构造函数 basic_directory_iterator()
老是构造一个 end 迭代器对象,它是惟一一个用于结束条件的合法迭代器。
示例代码,获得指定目录下的全部文件名:code
递归遍历目录的迭代器,它的构造参数与basic_directory_iterator相同,当调用 operator++
时,若是当前值是一个目录,则进入下一级目录。
它有三个成员函数:对象
函数名 | 做用 |
---|---|
int level() const; | 获得当前搜索深度 |
void pop(); | 调用pop()后,下一次递增就会直接返回上一级目录 |
void no_push(); | 调用no_push()后,即使下一个元素是目录类型也不进入 |
示例代码,获得指定目录下的全部文件名(和上例做用相同):递归