版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、boost常用库的使用介绍第三讲:boost文件系统库主讲人:步磊峰 UIPower 3D界面引擎负责人第一节: C+文件操作1、C语言运行库2、C+标准库3、win32 文件系统操作 API 总体来说,C+缺乏一个对操作系统中的文件进行查询和操作的能力, 而boost:filesystem库提供了一个可移植的文件系统操作库,使用 POSIX标准表示的文件系统路径,接口类似标准库的容器和迭代器, 以跨平台操作目录,文件。2第二节:filesystem库编译filesystem库需要system库支持,因此必须先编译system库 编译命令如下: b2 -with-system -with-fi
2、lesystem -toolset=msvc-9.0 variant=release link=static stage pause 运行上述批处理程序后,我们得到两个release版本的静态链接库,这两个静态链接库存放在stage/lib文件夹中 3第三节:路径、目录与文件1、路径:逻辑表示 网络路径: 1) / (size = 2 & 0 = / & 1 = /) 2) /net/abc (size 2 & 0 = / & 1 = / & 2 != / windows绝对路径: c:/foo poxis绝对路径: /foo/abc / 相对路径: foo/abc 2、目录:物理表示3、文件
3、:输入输出 4第四节:filesystem:path类1、path:prefered_separator(路径分隔符常量) windows posix / 2、path的构造、拷贝构造函数以及析构函数 path(); /构造一个空路径 path(const path& p); /拷贝构造函数 template path(Source const& source, const codecvt_type& cvt=codecvt(); /从字符串构造一个路径,必要时进行内码转换 template path(InputIterator begin, InputIterator end, const
4、codecvt_type& cvt=codecvt(); /从一个子串构造路径 path(); /析构函数 5第四节:filesystem:path类3、path的赋值操作 path& operator=(const path& p); template path& operator=(Source const& source); template path& assign(Source const& source, const codecvt_type& cvt) template path& assign(InputIterator begin, InputIterator end, co
5、nst codecvt_type& cvt=codecvt(); 6第四节:filesystem:path类4、path的追加 path& operator/=(const path& p); template path& operator/=(Source const& source); template path& append(Source const& source, const codecvt_type& cvt); template path& append(InputIterator begin, InputIterator end, const codecvt_type& cv
6、t=codecvt(); inline path operator/(const path& lhs, const path& rhs) return path(lhs) /= rhs; 对于追加的分隔符,windows下是,而posix下是/7第四节:filesystem:path类5、path的连接 path& operator+=(const path& x); path& operator+=(const string_type& x); path& operator+=(const value_type* x); path& operator+=(value_type x); tem
7、plate path& operator+=(Source const& x); template path& operator+=(CharT x); template path& concat(Source const& x, const codecvt_type& cvt); template path& concat(InputIterator begin, InputIterator end); template path& concat(InputIterator begin, InputIterator end, const codecvt_type& cvt);8第四节:fil
8、esystem:path类6、path的修改 void clear(); path& make_preferred(); / ISO/IEC 9945: no effect. Windows: convert slashes to backslashes path& remove_filename(); path& replace_extension(const path& new_extension = path(); void swap(path& rhs); 9第四节:filesystem:path类7、path的字符串表示 const string_type& native() con
9、st noexcept; / native format, encoding const value_type* c_str() const noexcept; / native().c_str() const string string(const codecvt_type& cvt=codecvt() const; const wstring wstring(const codecvt_type& cvt=codecvt() const; const string generic_string(const codecvt_type& cvt=codecvt() const; const w
10、string generic_wstring(const codecvt_type& cvt=codecvt() const; 8、path的比较操作 int compare(const path& p) const noexcept; int compare(const std:string& s) const; int compare(const value_type* s) const; 10第四节:filesystem:path类9、path的解析 10、path的查询 path root_name() const; bool has_root_name() const; path r
11、oot_directory() const; bool has_root_directory() const; path root_path() const; bool has_root_path() const; path relative_path() const; bool has_relative_path() const; path parent_path() const; bool has_parent_path() const; path filename() const; bool has_filename() const; path stem() const; bool ha
12、s_stem() const; path extension() const; bool has_extension() const;11第四节:filesystem:path类11 路径解析的优化 fs:path ret; if(_path.has_XXXX() ret = _path.XXX(); do_something(ret); /上述代码需要进行两次字符串扫描解析,更好的方式如下: fs:path ret = _path.XXX(); if(!ret.empty() do_something(ret); /上述代码仅需要一次字符串扫描解析12第四节:filesystem:path类
13、12、path的其他操作 bool empty() const; bool is_absolute() const; bool is_relative() const; iterator begin() const; iterator end() const; 13第四节:filesystem:path类 14第四节:filesystem:path类 15第四节:filesystem:path类 16第四节:filesystem:path类17第四节:filesystem:path类18第四节:filesystem:path类path.root_path()解析规则: root_path =
14、root_name() + root_directory()path.relative_path()解析规则: 返回root_path()之后的子字符串path.parent_path()解析规则: 返回 *-end()之前的子字符串path.filename()解析规则: 返回 *-end()字符串 19第五节:可移植文件名1、posix规范对文件名的限制 文件名仅可包含如下字符: 1)大小写字母(a-z & A-Z) 2)数字(0-9) 3)点号( . ) 4)下划线( _ ) 5)连字符 (- )2、windows系统对文件名的限制 windows对文件名字符使用范围要广一些,仅仅不允许
15、出现ascii 0 x0 - 0 x1F ? : | / 20第五节:可移植文件名3 filesystem库提供的文件名检测函数(name_check function) 21第六节:文件状态查询1、file_status类 file_type type() const; void type(file_type v); perms permissions() const; void permissions(perms prms); 2、file_type 枚举 status_error symlink_file type_unknown file_not_found block_file st
16、atus_unknown character_file regular_file fifo_file directory_file socket_file 22第六节:文件状态查询3、文件状态查询相关函数 file_status status(const path& p); /通用函数 bool exists(const path& p); /=status(p).type() != file_not_found bool is_directory (const path& p); /=status(p).type() =directory_file bool is_regular_file(
17、const path& p); /=status(p).type() = regular_file bool is_symlink(const path& p); /=status(p).type() = symlink_file bool is_other (const path& p); /当文件存在且不是普通文件,目录或链接文件时返回true bool is_empty (const path& p); /1) p为目录时,目录中无文件时则返回true /2) p为文件时,文件长度为0时则返回true 23第七节:文件属性操作文件属性操作相关函数 const path& initial_
18、path(); /只读函数,返回应用程序所在的目录,绝对路径表示 void current_path(const path& p); /设置当前路径 path current_path(); /如果没有调用上面的函数,返回的目录和initial_path一致 注意: 下面函数都要求操作的文件必须存在,否则会抛出异常。 uintmax_t file_size(const path& p); /以字节为单位返回文件的长度,文件类型必须是regular_file std:time_t last_write_time(const path& p); /返回文件的最后修改时间 void last_wri
19、te_time(const path& p, const std:time_t new_time); /设置文件最后修改的时间 struct space_info uintmax_t capacity , free, available; ; space_info space(const path& p); 24第八节:文件操作文件操作相关常用函数 bool create_directory(const path& p); bool create_directories(const path& p); void rename(const path& from, const path& to);
20、 bool remove(const path& p); uintmax_t remove_all(const path& p); void copy(const path& from, const path& to); void copy_directory(const path& from, const path& to); void copy_file(const path& from, const path& to, copy_option option); void resize_file(const path& p, uintmax_t size); 25第九节:目录迭代1 单层迭代器 class directory_iterator : public boost:iterator_facade public: directory_iterator() / creates the end iterator explicit directory_iterator(const path& p); directory_iterator& operator=(const directory_iterator&); directory_iterator& operator+(); 26第九节:目录迭代2 递归迭代器 class recursive_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年阀门零件加工操作工考试试卷及答案
- 2026年眼科 PDCA 改进案例测评试卷及答案
- 医院内静脉血栓栓塞症规范管理工作手册
- 注册安全工程师(中级)专业实务·化工 考前速记清单
- 2026年全国计算机等级考试三级网络技术题库及答案
- 2026秋小学(教科版广州新教材)英语一年级上册教学计划附教学进度表
- 2026年基层医疗机构院感防控管理能力提升培训考核试题及答案
- 2026年出库差错原因分析防范试卷(含答案)
- 2026秋小学统编版道德与法治三年级上册教学计划
- 刻瓷工创新思维竞赛考核试卷含答案
- (2026秋新版)部编版道德与法治四年级上册全册教案
- 第19课《培养德智体美劳全面发展的社会主义建设者和接班人》教学设计-2026-2027学年统编版九年级语文上册
- 2026年重庆市公务员真题和答案
- 2026年4月自考00037美学试题及答案含评分参考
- (新教材)2026年部编人教版一年级上册语文 6 j q x 课件
- 挖掘机租赁合同标准范本下载
- 减震垫片施工方案
- 责任心和执行力培训课件
- 2025年山西省教师职称考试(思想政治/道德与法治)历年参考题库含答案详解(5卷)
- 桉树买卖合同(2025版)
- 第9谭《艺术与科技的新结合》课件-2024-2025学年赣美版(2024)初中美术九年级上册
评论
0/150
提交评论