本文将针对常用的场景,对 std::filesystem 的使用逐一进行验证:

  1. 判断文件夹是否存在
  2. 创建单层目录
  3. 逐级创建多层目录
  4. 创建多级目录
  5. 当前文件路径
  6. 创建文件"from.dat"
  7. 获取相对于base的绝对路径
  8. 文件拷贝
  9. 移动文件或重命名
  10. 创建文件 “example.dat
  11. 获取文件大小
  12. 获取文件最后修改时间
  13. 删除文件
  14. 递归删除目录下所有文件
  15. 在临时文件夹下创建文件夹并删除

一、Cpp 17 的支持

cppreference - filesystem

# Sample CMakeLists.txt
cmake_minimum_required(VERSION 3.21) # Define a CMake project project(
simple_executable_fileSystem
VERSION 1.0
DESCRIPTION "A simple C++ project to demonstrate basic CMake usage"
LANGUAGES CXX) # include_directories(include) # find_package(fmt CONFIG REQUIRED)
find_package(fmt QUIET)
if (NOT fmt_FOUND)
message(WARNING "fmt not found, skipping vcpkg example")
endif () add_executable(simple_executable_with_vcpkg) target_include_directories(simple_executable_with_vcpkg
PUBLIC include
) target_compile_features(
simple_executable_with_vcpkg
PRIVATE cxx_std_17 # 设定 CXX 17 标准
) target_sources(
simple_executable_with_vcpkg
PRIVATE src/main.cpp
) target_link_libraries(
simple_executable_with_vcpkg
PRIVATE fmt::fmt-header-only
)

二、头文件和命名空间

#include<filesystem>
using namespace std::filesystem;

三、常用类

path 类:说白了该类只是对字符串(路径)进行一些处理,这也是文件系统的基石。

directory_entry 类:功如其名,文件入口,这个类才真正接触文件。

directory_iterator 类:获取文件系统目录中文件的迭代器容器,其元素为 directory_entry对象(可用于遍历目录)

file_status 类:用于获取和修改文件(或目录)的属性(需要了解C++11的强枚举类型(即枚举类))

四、使用用法

  1. 需要有一个path对象为基础,如果需要修改路径,可以调用其成员函数进行修改(注意其实只是处理字符串)。
  2. 需要获取文件信息需要通过path构造directory_entry,但需要path一定存在才能调用构造,所以需要实现调用exists(path .)函数确保目录存在才能构造directory_entry(注意文件入口中的exists无法判断)。
  3. 若需遍历,则可以使用 directory_iterator,进行遍历

Samples

// Sample 1
#include <iostream>
#include <filesystem> using namespace std;
using namespace std::filesystem; int main() {
path str("C:\\Windows");
if (!exists(str)) //必须先检测目录是否存在才能使用文件入口.
return 1;
directory_entry entry(str); //文件入口
if (entry.status().type() == file_type::directory) //这里用了C++11的强枚举类型
cout << "该路径是一个目录" << endl;
directory_iterator list(str); //文件入口容器
for ( auto & it : list )
cout << it.path().filename() << endl; //通过文件入口(it)获取path对象,再得到path对象的文件名,将之输出
system("pause");
return 0;
}
// Sample 2
#include <fmt/core.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <cassert> namespace fs = std::filesystem; int main() {
// 1> 判断文件夹是否存在
std::string dirName{ "log" };
fs::path url(dirName);
if (!fs::exists(url))
{
// fmt::print("{} is not exist\n", std::quoted(dirName)); // CXX14 引入std::quoted用于给字符串添加双引号
fmt::print("\"{}\" is not exist\n", dirName);
}
else
{
fmt::print("\"{}\" is exist\n", dirName);
} // <2> 创建单层目录 in build directories
bool okey = fs::create_directories(dirName);
fmt::print("create_directories({}), result = {}\n", dirName, okey); // <3> 逐级创建多层目录
std::error_code err;
std::string subDir = dirName + "/subdir";
okey = fs::create_directories(subDir, err);
fmt::print("create_directories({}), result = {}\n", subDir, okey);
fmt::print("err.value() = {}, err.message() = {}\n", err.value(), err.message()); // <4> 创建多级目录
dirName = "a/b//c/d/";
okey = fs::create_directories(dirName, err);
fmt::print("create_directories({}), result = {}\n", dirName, okey);
fmt::print("err.value() = {}, err.message() = {}\n", err.value(), err.message()); // <5> 当前文件路径
fs::path currentPath = fs::current_path(); // D:\Coding\CLion\Cpp17-Complete-Guide\build\simple_executable
fmt::print("currentPath = {}\n", currentPath.string());
fmt::print("root_directory = {}\n", currentPath.root_directory().string());
fmt::print("relative_path = {}\n",
currentPath.relative_path().string()); // Coding\CLion\Cpp17-Complete-Guide\build\simple_executable
fmt::print("root_name = {}\n", currentPath.root_name().string());
fmt::print("root_path = {}\n", currentPath.root_path().string()); // <6> 创建文件"from.dat"
fs::path oldPath(fs::current_path() / "from.dat");
std::fstream file(oldPath, std::ios::out | std::ios::trunc);
if (!file)
{
fmt::print("Create file ({}) failed!!!\n", oldPath.string());
}
file.close(); // <7> 获取相对于base的绝对路径
fs::path absPath = fs::absolute(oldPath/*, fs::current_path()*/);
fmt::print("absPath = {}\n", absPath.string()); // <8> 文件拷贝
fs::create_directories(fs::current_path() / "to");
fs::path toPath(fs::current_path() / "to/from0.dat");
fs::copy(oldPath, toPath); // <9> 移动文件或重命名
fs::path newPath(fs::current_path() / "to/to.dat");
fs::rename(oldPath, newPath); // <10> 创建文件 "example.dat"
fs::path _path = fs::current_path() / "example.dat";
fmt::print("example.data path: {}\n", _path.string());
std::ofstream(_path).put('a'); // create file of size 1
std::ofstream(_path).close(); // 文件类型判定
assert(fs::file_type::regular == fs::status(_path).type()); // <11> 获取文件大小
auto size = fs::file_size(_path);
fmt::print("file_size = {}\n", size); // <12> 获取文件最后修改时间
auto time = fs::last_write_time(_path);
fmt::print("last_write_time = {}\n", time.time_since_epoch().count()); // <13> 删除文件
okey = fs::remove(_path);
fmt::print("remove ({})\n", _path.string()); // <14> 递归删除目录下所有文件,返回被成功删除的文件个数
uintmax_t count = fs::remove_all(dirName);//dirName="a/b//c/d/",会把d目录也删掉
fmt::print("remove_all ({}), {}\n", dirName, count); // <15> 在临时文件夹下创建文件夹并删除
fs::path tmp = fs::temp_directory_path();//"C:\Users\Kandy\AppData\Local\Temp\"
fmt::print("temp_directory_path = {}\n", tmp.string());
fs::create_directories(tmp / "_abcdef/example");
std::uintmax_t n = fs::remove_all(tmp / "_abcdef");
fmt::print("Deleted {} files or directories\n", n); return 0;
}

五、常用库函数

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这样的,如果都不存在,创建目录结构

bool exists(const path& pval) :用于判断path是否存在

uintmax_t file_size(const path& pval) :返回目录的大小

file_time_type last_write_time(const path& pval) :返回目录最后修改日期的file_time_type对象

bool remove(const path& pval) :删除目录

uintmax_t remove_all(const path& pval) :递归删除目录下所有文件,返回被成功删除的文件个数

void rename(const path& from, const path& to) :移动文件或者重命名

库函数 | C++17 std::filesystem文件系统 用法指北的更多相关文章

  1. C++17 std::shared_mutex的替代方案boost::shared_mutex

    C++17 std::shared_mutex的替代方案boost::shared_mutex C++17boost  std::shared_mutex http://en.cppreference ...

  2. Python 简单入门指北(二)

    Python 简单入门指北(二) 2 函数 2.1 函数是一等公民 一等公民指的是 Python 的函数能够动态创建,能赋值给别的变量,能作为参传给函数,也能作为函数的返回值.总而言之,函数和普通变量 ...

  3. Python 简单入门指北(一)

    Python 简单入门指北(一) Python 是一门非常容易上手的语言,通过查阅资料和教程,也许一晚上就能写出一个简单的爬虫.但 Python 也是一门很难精通的语言,因为简洁的语法背后隐藏了许多黑 ...

  4. 可能比文档还详细--VueRouter完全指北

    可能比文档还详细--VueRouter完全指北 前言 关于标题,应该算不上是标题党,因为内容真的很多很长很全面.主要是在官网的基础上又详细总结,举例了很多东西.确保所有新人都能理解!所以实际上很多东西 ...

  5. 颓废选手在 Ubuntu/Noilinux 下的生存指北

    颓废选手在 Ubuntu/Noilinux 下的生存指北 Hint: 这里的 "#" 都是假注释,复制的时候记得删除 一些基本的生存命令 ctrl + alt + t #调出终端 ...

  6. Markdown 标记语言指北 - 源码

    这是上一篇博客的源代码. 这是班刊约稿的一篇文章. 全文约6000字, 预计需要 60 分钟读完. # Markdown 标记语言指北 #### TOC 1. [什么是 Markdown?](#%E4 ...

  7. ansible使用指北(二)

    前言在上一篇文章里我们了解了ansible的常用模块,今天我们来了解下ansible-playbook,ansbile-playbook是一系统ansible命令的集合,其利用yaml 语言编写,an ...

  8. WEB安全漏洞挖掘向入坑指北

    这个指北不会给出太多的网站和方向建议,因为博主相信读者能够从一个点从而了解全局,初期的时候就丢一大堆安全网址导航只会浇灭人的热情,而且我也不适合传道授业解惑hhh 安全论坛: 先知社区 freebuf ...

  9. git宝典—应付日常工作使用足够的指北手册

    最近公司gitlab又迁移,一堆git的命令骚操作,然鹅git命令,感觉还是得复习下——其实,git现在界面操作工具蛮多,比如intellij 自带的git操作插件就不错,gitlab github ...

  10. 关于supervisor的入门指北

    关于supervisor的入门指北 在目前这个时间点(2017/07/25),supervisor还是仅支持python2,所以我们要用版本管理pyenv来隔离环境. pyenv 根据官方文档的讲解, ...

随机推荐

  1. 每天5分钟复习OpenStack(十一)Ceph部署

    在之前的章节中,我们介绍了Ceph集群的组件,一个最小的Ceph集群包括Mon.Mgr和Osd三个部分.为了更好地理解Ceph,我建议在进行部署时采取手动方式,这样方便我们深入了解Ceph的底层.今天 ...

  2. Git 安装与配置教程

    一.下载Git 1.官网地址 下载地址 2.下载 二.安装 1.下载完成后,直接点击安装包安装,即可. 2.开始安装,然后下一步 3.可以在此处自定义地址,然后下一步 3.选择组件 此处默认即可,但我 ...

  3. 4 HTTP的“四层”和“七层”

    目录 1 四层:TCP/IP 网络分层模型 2 七层:OSI网络分层模型 3 TCP/IP 协议栈的工作方式 1 四层:TCP/IP 网络分层模型 四层是指TCP/IP 网络分层模型. 第一层:&qu ...

  4. 整合SpringBoot + Dubbo + Nacos 出现 Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass

    版本 SpringBoot:2.7.3 Dubbo:3.0.4 Nacos:2.0.3 异常信息如下 Unable to make protected final java.lang.Class ja ...

  5. MySQL搭建主从集群详细步骤~

    一. Docker安装MySQL搭建主从 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] docker run -p 3306:3306 很多 -d --n ...

  6. [.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle

    前言 今天给大家分享一个方便易用的.NET在线代码编辑工具,能够帮助.NET开发人员快速完成代码编写.测试和分享的需求(.NET开发者的福音):.NET Fiddle. .NET Fiddle介绍 我 ...

  7. 华企盾DSC造成svn、git连接不上常见处理方法

    1.检查svn服务器是否正在运行 2.检查个人模式连接不上服务器网络加密了客户端未加密(查看客户端日志进程是否为legal:1网络访问设置是否正常,试试只加密服务器IP及端口的方式),个人模式可以连接 ...

  8. 数字孪生和GIS系统融合能为水利领域带来什么改变?

    随着科技的不断进步,数字孪生和GIS系统的融合应用逐渐成为了水利领域的新趋势.数字孪生是指通过数字化技术模拟物理实体和过程,将现实世界与虚拟世界相结合的技术,而GIS系统则是地理信息系统,用于收集.存 ...

  9. Javascript Ajax总结——HTTP头部信息

    每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用,有的没用.XHR对象也提供了操作这两种头部(即请求头部和响应头部)信息的方法.默认情况下,在发送XHR请求的同时,还会发送下列头部 ...

  10. 5 款开源热搜项目「GitHub 热点速览」

    随着 2023 年的结束,我们也迎来了 2024 年的第一个工作日,新的一年就让「GitHub 热点速递」陪你一起进入工作状态吧! 说到上周的 GitHub 热搜项目就不得不提一下,一周飙升了 8 千 ...