boost-文件路径操作filesystem
来源:互联网 发布:数据分析图表软件 编辑:程序博客网 时间:2024/06/16 06:23
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成员函数:
bool has_root_name() const;
bool has_root_directory() const;
bool has_relative_path() const;
bool has_filename() const;
bool has_branch_path() const;路经中是否包含指定的项
测试代码:
- #include "boost/filesystem.hpp" // 包含所有需要的 Boost.Filesystem 声明
- #include <iostream> // 使用 std::cout
- namespace fs = boost::filesystem;
- // 宏FSTEST:测试f的成员函数,输出成员函数名和结果
- #define FSTEST(x) std::cout << #x##": " << f.x << std::endl
- int main()
- {
- fs::path f("\\folder1\\folder2\\folder3\\filename.ext");
- FSTEST(string());
- FSTEST(file_string());
- FSTEST(directory_string());
- FSTEST(root_name());
- FSTEST(root_directory());
- FSTEST(root_path());
- FSTEST(relative_path());
- FSTEST(filename());
- FSTEST(parent_path());
- FSTEST(stem());
- FSTEST(extension());
- FSTEST(replace_extension("new"));
- char buf[]="hello";
- FSTEST(append(buf, buf+sizeof(buf)));
- FSTEST(remove_filename());
- return 0;
- }
输出:
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/
常用函数
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 迭代器对象,它是唯一一个用于结束条件的合法迭代器。
示例代码,得到指定目录下的所有文件名:
- void find_file( const fs::path & dir_path )
- {
- if ( !fs::exists( dir_path ) ) return;
- fs::directory_iterator end_itr; // 缺省构造生成一个结束迭代器
- for ( fs::directory_iterator itr( dir_path );
- itr != end_itr;
- ++itr )
- {
- if ( fs::is_directory(itr->status()) )
- {
- find_file( itr->path() ); //递归查找
- }
- else
- {
- std::cout << *itr << std::endl;
- }
- }
- }
basic_recursive_directory_iterator
递归遍历目录的迭代器,它的构造参数与basic_directory_iterator相同,当调用 operator++
时,如果当前值是一个目录,则进入下一级目录。
它有三个成员函数:
示例代码,得到指定目录下的所有文件名(和上例作用相同):
- void find_file2( const fs::path & dir_path )
- {
- fs::recursive_directory_iterator end_itr; // 缺省构造生成一个结束迭代器
- for ( fs::recursive_directory_iterator itr( dir_path );
- itr != end_itr;
- ++itr )
- {
- std::cout << itr.level() << *itr << std::endl;
- }
- }
原文地址:http://www.cnblogs.com/lidabo/p/3955717.html
- boost-文件路径操作filesystem
- Boost Filesystem Library 路径 文件 目录
- Boost学习之可移植路径操作--filesystem
- boost之filesystem库,目录文件操作函数
- boost::filesystem的基本操作
- boost::filesystem的文件遍历
- boost::filesystem与CFile文件打开方式
- Boost.Filesystem 使用 以及文件时间
- boost.filesystem
- Boost.Filesystem
- boost filesystem
- Boost.Filesystem
- Boost::Filesystem
- boost filesystem
- boost::filesystem
- hadoop中的文件操作 FileSystem
- php文件操作技巧FileSystem
- (九)boost库之文件处理filesystem
- dropzone.js图片上传
- 何金昌内增高鞋 专业打造专属时尚范儿网厄瓜多尔1季度皮革生产价格指数涨幅明显
- 最简单的设计模式---单例模式
- 你不得不知的KISS原则
- SQL调优(SQL TUNING)之远程支持完成性能大幅优化
- boost-文件路径操作filesystem
- SQL Programming (4):Using the SQL Model Classes
- 终极报表设计
- IOS单例模式(MRC下和ARC下)
- iOS中Lable的自适应高
- awk_对某一列进行去重
- dp经典题目
- 利用Window 设置activity的全屏
- json 的解析-使用Android中的JSONObject和JSONArray解析json数据