【小沐学C++】C++17实现文件操作<filesystem>
文章目录
- 1、简介
- 1.1 C/C++标准种类
- 1.2 VS对C++标准的支持情况
- 1.3 gcc对C++标准的支持情况
- 2、头文件<filesystem>介绍
- 2.1 类(Classes)
- 2.2 结构(Structs)
- 2.3 函数(Functions)
- 2.4 操作符(Operators)
- 2.5 枚举(Enumerations)
- 3、头文件<filesystem>的函数
- 4、头文件<filesystem>的path类
- 5、to_string函数
- 结语
1、简介
<filesystem>是用于访问操作和检索有关路径、文件和目录的信息的类和函数的头文件。
在 Visual Studio 2017 发布时,<filesystem>头文件还不是 C++ 标准。Visual Studio 2017 RTW 中的 C++ 实现了ISO/IEC JTC 1/SC 22/WG 21 N4100中的最终草案标准。Visual Studio 2017 版本 15.7 及更高版本支持新的 C++17<filesystem>标准。这是一个全新的实现,与以前的std::experimental版本不兼容。符号链接支持、错误修复和标准所需行为的更改使其成为必要。在 Visual Studio 2019 版本 16.3 及更高版本中,包括<filesystem>仅提供新的std::filesystem. 包括<experimental/filesystem>仅提供旧的实验性实现。实验性实现将在库的下一个 ABI 破坏版本中删除。
1.1 C/C++标准种类
- ⽬前C语⾔的标准有:C89(ANSI C)、C90、C95、C99(ISO C)、C11(C1x)
- ⽬前C++语⾔的标准有:C++98、C++03(对98⼩幅修改)、C++11(全⾯进化)、C++14、C++17
1.2 VS对C++标准的支持情况
- C++17
VS2017基本支持,VS2015部分支持。 - C++14
VS2017可以完全支持,VS2015基本支持,VS2013部分支持。 - C++11
VS2015及以上完全支持。VS2013基本支持,VS2012部分支持,VS2010及以下版本不支持。
#ifdef __cpp_lib_filesystem
#pragma message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
namespace std {namespace filesystem = experimental::filesystem;
}
#endif
/Zc:__cplusplus std::cout << __cplusplus << std::endl;
1.3 gcc对C++标准的支持情况
2、头文件<filesystem>介绍
https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-functions?view=msvc-170
2.1 类(Classes)
Name | Description |
---|---|
directory_entry class | Describes an object that is returned by a directory_iterator or a recursive_directory_iterator and contains a path. |
directory_iterator class | Describes an input iterator that sequences through the file names in a file-system directory. |
filesystem_error class | A base class for exceptions that are thrown to report a low-level system overflow. |
path class | Defines a class that stores an object of template type String that is suitable for use as a file name. |
recursive_directory_iterator class | Describes an input iterator that sequences through the file names in a file-system directory. The iterator can also descend into subdirectories. |
file_status class | Wraps a file_type. |
2.2 结构(Structs)
Name | Description |
---|---|
space_info structure | Holds information about a volume. |
struct space_info
{uintmax_t capacity;uintmax_t free;uintmax_t available;
};
2.3 函数(Functions)
https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-functions?view=msvc-170
2.4 操作符(Operators)
https://docs.microsoft.com/en-us/cpp/standard-library/filesystem-operators?view=msvc-170
Name | Description |
---|---|
operator== | bool operator==(const path& left, const path& right) noexcept; |
operator!= | bool operator!=(const path& left, const path& right) noexcept; |
operator< | bool operator<(const path& left, const path& right) noexcept; |
operator<= | bool operator<=(const path& left, const path& right) noexcept; |
operator> | bool operator>(const path& left, const path& right) noexcept; |
operator>= | bool operator>=(const path& left, const path& right) noexcept; |
operator/ | path operator/(const path& left, const path& right); |
operator<< | template <class Elem, class Traits> basic_ostream<Elem, Traits>& operator<<(basic_ostream<Elem, Traits>& os, const path& pval); |
operator>> | template <class Elem, class Traits> basic_istream<Elem, Traits>& operator<<(basic_istream<Elem, Traits>& is, const path& pval); |
2.5 枚举(Enumerations)
Name | Description |
---|---|
copy_options | An enumeration that is used with copy_file and determines behavior if a destination file already exists. |
directory_options | An enumeration that specifies options for directory iterators. |
file_type | An enumeration for file types. |
perm_options | Enumerates options for the permissions function. |
perms | A bitmask type used to convey permissions and options to permissions |
3、头文件<filesystem>的函数
- <filesystem>常用的成员函数如下:
函数名 | 功能 |
---|---|
void copy(const path& from, const path& to) | 目录复制 |
path absolute(const path& pval, const path& base = current_path()) | 获取相对于base的绝对路径 |
bool create_directory(const path& pval) | 当目录不存在时创建目录 |
bool create_directories(const path& pval) | 形如/a/b/c这样的,如果都不存在,创建目录结构 |
uintmax_t file_size(const path& pval) | 返回目录的大小 |
file_time_type last_write_time(const path& pval) | 返回目录最后修改日期的file_time_type对象 |
bool exists(const path& pval) | 用于判断path是否存在 |
bool remove(const path& pval) | 删除目录 |
uintmax_t remove_all(const path& pval) | 递归删除目录下所有文件,返回被成功删除的文件个数 |
void rename(const path& from, const path& to) | 移动文件或者重命名 |
#include <iostream>#ifdef __cpp_lib_filesystem
#pragma message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
#endif
namespace fs = std::filesystem;int main() {std::cout << __cplusplus << std::endl;fs::current_path(fs::temp_directory_path());fs::create_directories("test2022/1/2/a");fs::create_directory("test2022/1/2/b");fs::permissions("test2022/1/2/b", fs::perms::others_all, fs::perm_options::remove);fs::create_directory("test2022/1/2/c", "test2022/1/2/b");std::system("tree test2022");fs::remove_all("test2022");return 0;
}
4、头文件<filesystem>的path类
- path类常用的成员函数如下:
函数名 | 功能 |
---|---|
path& append(const _Src& source) | 在path末尾加入一层结构 |
path& assign(string_type& source) | 赋值(字符串) |
void clear() | 清空 |
int compare(const path& other) | 进行比较 |
bool empty() | 空判断 |
path filename() | 返回文件名(有后缀) |
path stem() | 返回文件名(不含后缀) |
path extension() | 返回文件后缀名 |
path is_absolute() | 判断是否为绝对路径 |
path is_relative() | 判断是否为相对路径 |
path relative_path() | 返回相对路径 |
path parent_path() | 返回父路径 |
path& replace_extension(const path& replace) | 替换文件后缀 |
#include <iostream>
#include <set>
#ifdef __cpp_lib_filesystem
#pragma message("__cpp_lib_filesystem: supported.")
#include <filesystem>
#endif
#ifdef __cpp_lib_experimental_filesystem
#pragma message("__cpp_lib_experimental_filesystem: supported.")
#include <experimental/filesystem>
#endif
namespace fs = std::filesystem;int main() {std::cout << __cplusplus << std::endl;fs::path src_dir("D:\\test");std::set<string> dir_set;for (fs::directory_iterator end, ite(src_dir); ite != end; ++ite){if (!fs::is_directory(ite->path()))dir_set.insert(ite->path().filename().string());};return 0;
}
5、to_string函数
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val)
结语
如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;
╮( ̄▽ ̄)╭
如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;
o_O???
如果您需要相关功能的代码定制化开发,可以留言私信作者;
(✿◡‿◡)
感谢各位童鞋们的支持!
( ´ ▽´ )ノ ( ´ ▽´)っ!!!
【小沐学C++】C++17实现文件操作<filesystem>相关推荐
- 零基础学lua第十天---文件操作
零基础学lua第十天-文件操作 一.前言 每个编程语言和都少不了对文件操作,lua中操作文件的方法非常简单,而且文件和输入输出操作都是使用的同一个库,可以说是相当方便了,下面我们来看下吧. 二.读取文 ...
- 【小沐学Python】Python实现在线电子书(MkDocs + readthedocs + github + Markdown)
文章目录 1.简介 2.安装 3.创建新项目 4.添加页面 5.编辑导航页 6.设置主题 7.更改图标图标 8.构建网站 9.部署 9.1 准备github项目 9.2 注册登录Read the Do ...
- 【小沐学python】(一)Python开发准备
<爱看书的小沐和爱发呆的戈戈>第一季 目录 1.<出生篇> 1.1 python历史背景 1.2 python职业方向 1.3 python语言排名 2.<巡山篇> ...
- 【小沐学Unity3d】Unity3d导入3D模型
文章目录 1.简介 1.1 标准文件格式 1.2 专有文件格式 1.3 从 Autodesk® 3ds Max® 导入对象 1.4 从 Blender 导入对象 2.测试 2.1 新建项目 2.2 导 ...
- 【小沐学Python】Python实现在线电子书(Sphinx + readthedocs + github + Markdown)
文章目录 1.简介 2.安装 3.创建测试工程 4.项目文件结构 5.编译为本地文件 6.编译为http服务 7.更改样式主题 8.支持markdown 9.修改文档显示结构 10.项目托管到gith ...
- 【小沐学C#】WPF中嵌入web网页控件(WebBrowser、WebView2、CefSharp)
文章目录 1.简介 1.1 WPF简介 1.2 WPF 体系结构 1.3 WPF入门开发 2.WebBrowser 2.1 WebBrowser特点 2.2 WebBrowser常用的属性.方法和事件 ...
- 【跟李沐学AI学习笔记】数据操作
本文的来源是B站跟李沐学AI的视频. 机器学习用的最多的数据结构是N维数组.最简单的N维数组是一个0-d的标量,比如1.0,它可能表示一个物体的类别.1-d的数组称为向量,比如说[1.0, 2.7, ...
- “笨办法”学Python 3基础篇-文件操作
"笨办法"学Python 3基础篇系列文章 "笨办法"学Python 3基础篇 第一部分-打印与输入 "笨办法"学Python 3基础篇 第 ...
- 【小沐学C++】C++17实现文件读写
本节内容 1.C library function 2.C++ standard library 2.1 常用文件读写 2.2 一次读取整个文件 3.CFile 类(MFC) 4.CStdioFile ...
最新文章
- 机器学习十大算法(二)
- linux系统运行快捷键,Linux操作系统下运行命令时CTRL+Z的作用
- 那些《西游记》中你不知道的野史,信不信由你
- 如何防止通过url攻击_什么是XSS攻击?如何防御XSS攻击?
- 把一个dataset的表放在另一个dataset里面_现在开始:用你的Mac训练和部署一个图像分类模型...
- bootstrapTable表格分页后,处理逻辑后刷新跳回第一页,没留在当前页的解决办法
- 剑指Offer55-II题解-平衡二叉树
- mssql 性能优化的一些认识
- Linux 基本命令(七)--cat,less,more,head,tail,nl 常用命令
- 2021年汽车电子之传感器行业研究报告
- 微信浏览器下载音频文件php,教你怎么下载公众号里面的音频文件
- vue + UEditor 上传图片(回显),上传附件 含token
- 肖博高中高考数学答题技巧方法及常见问题
- 总论点和分论点_将破坏性的论点变成富有成效的对话
- RFID固定资产条码标签管理系统,使资产更易盘点
- 教育培训行业使用CRM管理系统有什么好处
- 求生之路2服务器模式修改参数,求生之路2参数修改控制台命令_求生之路2参数修改控制台命令方法_牛游戏网...
- linux查看docker是否启动命令行,如何通过命令行查看docker服务是否已启动
- 【学生网页设计作品 】关于HTML公益主题网页设计——关爱空巢老人 5页 带表单
- SHA-1和SHA-2算法有什么区别?