IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> C++17实现文件操作<filesystem> -> 正文阅读

[开发工具]C++17实现文件操作<filesystem>

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)

NameDescription
directory_entry classDescribes an object that is returned by a directory_iterator or a recursive_directory_iterator and contains a path.
directory_iterator classDescribes an input iterator that sequences through the file names in a file-system directory.
filesystem_error classA base class for exceptions that are thrown to report a low-level system overflow.
path classDefines a class that stores an object of template type String that is suitable for use as a file name.
recursive_directory_iterator classDescribes an input iterator that sequences through the file names in a file-system directory. The iterator can also descend into subdirectories.
file_status classWraps a file_type.

2.2 结构(Structs)

NameDescription
space_info structureHolds 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

NameDescription
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)

NameDescription
copy_optionsAn enumeration that is used with copy_file and determines behavior if a destination file already exists.
directory_optionsAn enumeration that specifies options for directory iterators.
file_typeAn enumeration for file types.
perm_optionsEnumerates options for the permissions function.
permsA 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???
如果您需要相关功能的代码定制化开发,可以留言私信作者;(????)
感谢各位童鞋们的支持!( ′ ▽′ )ノ ( ′ ▽′)っ!!!

在这里插入图片描述

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-06-20 23:07:02  更:2022-06-20 23:07:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/20 21:26:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码