C++17中std::filesystem::directory_entry的使用
C++17引入了std::filesystem库(文件系统库, filesystem library)。这里整理下std::filesystem::directory_entry的使用。
std::filesystem::directory_entry,目录项,获取文件属性。此directory_entry类主要用法包括:
(1).构造函数、operator=、assign:赋值;
(2).replace_filename: 更改目录项的文件名;
(3).path: 返回std::filesystem::path对象;
(4).exists: 检查指定的目录项是否存在;
(5).is_block_file、is_character_file: 检查目录项是否是块设备(block device)、字符设备(character device);
(6).is_directory: 检查目录项是否是目录;
(7).is_fifo: 检查目录项是否是命令管道;
(8).is_other: 检查目录项是否是其它文件(不是常规文件、目录或符号链接);
(9).is_regular_file: 检查目录项是否是常规文件;
(10).is_socket: 检查目录项是否是命名套接字;
(11).is_symlink: 检查目录项是否是符号链接;
(12).file_size: 获取目录项指定的文件大小;
(13).last_write_time:获取目录项最后修改时间。
以下为测试代码:注意windows和linux结果输出的差异
namespace {float get_file_size(std::uintmax_t size, std::string& suffix)
{float s1 = size / 1024. / 1024 / 1024;float s2 = size / 1024. / 1024;float s3 = size / 1024.;if (s1 > 1) {suffix = " GB";return s1;}if (s2 > 1) {suffix = " MB";return s2;}if (s3 > 1) {suffix = " KB";return s3;}suffix = " Bytes";return size;
}std::string to_time_t(std::filesystem::file_time_type tp)
{using namespace std::chrono;auto sctp = time_point_cast<system_clock::duration>(tp - std::filesystem::file_time_type::clock::now() + system_clock::now());auto tt = system_clock::to_time_t(sctp);std::tm* gmt = std::localtime(&tt); // UTC: std::gmtime(&tt);std::stringstream buffer;buffer << std::put_time(gmt, "%Y-%m-%d %H:%M:%S");return buffer.str();
}} // namespaceint test_filesystem_directory_entry()
{namespace fs = std::filesystem;// 1. construct,operator=,assignfs::directory_entry d1{ fs::current_path() };fs::directory_entry d2 = d1;fs::directory_entry d3;d3.assign(fs::current_path());if ((d1 == d2) && (d1 == d3))std::cout << "they are equal" << std::endl; // they are equal// windows: d1:"E:\\GitCode\\Messy_Test\\prj\\x86_x64_vc12\\CppBaseTest"// linux: d1:"/home/spring/GitCode/Messy_Test/prj/linux_cmake_CppBaseTest"std::cout << "d1:" << d1 << std::endl;// 2. replace_filenamed1.replace_filename("C++17Test");// windows: d1:"E:\\GitCode\\Messy_Test\\prj\\x86_x64_vc12\\C++17Test"// linux: d1:"/home/spring/GitCode/Messy_Test/prj/C++17Test"std::cout << "d1:" << d1 << std::endl;// 3. pathfs::path p1 = d1.path();// windows: p1:"E:\\GitCode\\Messy_Test\\prj\\x86_x64_vc12\\C++17Test"// linux: p1:"/home/spring/GitCode/Messy_Test/prj/C++17Test"std::cout << "p1:" << p1 << std::endl;// 4. existsfor (const auto& str : { "C:\\Program Files (x86)", "/usr/local" , "E:\\GitCode\\xxx", "/usr/xxx"}) {fs::directory_entry entry{ str };/* windows:directory entry: "C:\\Program Files (x86)":existsdirectory entry: "/usr/local":does not existdirectory entry: "E:\\GitCode\\xxx":does not existdirectory entry: "/usr/xxx":does not exist *//* linux:directory entry: "C:\\Program Files (x86)":does not existdirectory entry: "/usr/local":existsdirectory entry: "E:\\GitCode\\xxx":does not existdirectory entry: "/usr/xxx":does not exist*/std::cout << "directory entry: " << entry << (entry.exists() ? ":exists\n" : ":does not exist\n");}// 5. is_block_file,is_character_file,is_directory,is_fifo,is_other,is_regular_file,is_socket,is_symlinkfor (const auto& str : { "/dev/null", "C:\\Program Files (x86)", "/usr/include/time.h", "C:\\MinGW\\bin\\c++filt.exe","/usr/bin/g++", "/dev/block/11:0"}) {fs::directory_entry entry{ str };/* windows:"C:\\Program Files (x86)" is a directory"C:\\MinGW\\bin\\c++filt.exe" is a regular_file *//* linux:"/dev/null" is a character device"/dev/null" is an other file"/usr/include/time.h" is a regular_file"/usr/bin/g++" is a regular_file"/usr/bin/g++" is a symlink"/dev/block/11:0" is a block device"/dev/block/11:0" is an other file"/dev/block/11:0" is a symlink */if (entry.is_block_file())std::cout << entry << " is a block device" << std::endl;if (entry.is_character_file())std::cout << entry << " is a character device" << std::endl;if (entry.is_directory())std::cout << entry << " is a directory" << std::endl;if (entry.is_fifo())std::cout << entry << " is a FIFO" << std::endl;if (entry.is_other())std::cout << entry << " is an other file" << std::endl;if (entry.is_regular_file())std::cout << entry << " is a regular_file" << std::endl;if (entry.is_socket())std::cout << entry << " is a named socket" << std::endl;if (entry.is_symlink())std::cout << entry << " is a symlink" << std::endl;}// 6. file_size, last_write_timefor (const auto& str : { "/usr/bin/g++", "D:\\FSCapture.exe", "D:\\DownLoad\\tmp.txt", "/usr/bin/cmake", "E:\\yulong.mp4"}) {fs::directory_entry entry{ str };/* windows:"D:\\FSCapture.exe" size: 2.82 MB"D:\\FSCapture.exe" last write time: 2016-03-29 09:26:26"D:\\DownLoad\\tmp.txt" size: 10 Bytes"D:\\DownLoad\\tmp.txt" last write time: 2023-09-26 09:00:35"E:\\yulong.mp4" size: 1.35 GB"E:\\yulong.mp4" last write time: 2023-08-19 22:42:56 *//* linux:"/usr/bin/g++" size: 910.82 KB"/usr/bin/g++" last write time: 2023-05-13 15:52:47"/usr/bin/cmake" size: 6.43 MB"/usr/bin/cmake" last write time: 2022-08-17 18:44:05 */if (entry.is_regular_file()) {std::string suffix;auto value = get_file_size(entry.file_size(), suffix);if (suffix == " Bytes")std::cout << entry << " size: " << static_cast<int>(value) << suffix << std::endl;elsestd::cout << entry << " size: " << std::fixed << std::setprecision(2) << value << suffix << std::endl;std::cout << entry << " last write time: " << to_time_t(entry.last_write_time()) << std::endl;}}return 0;
}
执行结果如下图所示:

GitHub:https://github.com/fengbingchun/Messy_Test
相关文章:
C++17中std::filesystem::directory_entry的使用
C17引入了std::filesystem库(文件系统库, filesystem library)。这里整理下std::filesystem::directory_entry的使用。 std::filesystem::directory_entry,目录项,获取文件属性。此directory_entry类主要用法包括: (1).构造函数、…...
C/C++跨平台构建工具CMake入门
文章目录 1.概述2.环境准备2.1 安装编译工具2.2 安装CMake 3.编译一个示例程序总结 1.概述 本人一直对OpenGL的3d渲染很感兴趣,但是苦于自己一直是Android开发,没有机会接触这方面的知识。就在最近吗,机会来了,以前一个做3D渲染的…...
【CFD小工坊】浅水方程的离散及求解方法
【CFD小工坊】浅水方程的离散及求解方法 前言基于有限体积法的方程离散界面通量与源项计算干-湿网格的处理数值离散的稳定性条件参考文献 前言 我们模型的控制方程,即浅水方程组的表达式如下: ∂ U ∂ t ∂ E ( U ) ∂ x ∂ G ( U ) ∂ y S ( U ) U…...
第十四章 类和对象——C++对象模型和this指针
一、成员变量和成员函数分开存储 在C中,类内的成员变量和成员函数分开存储 只有非静态成员变量才属于类的对象上 class Person {public:Person() {mA 0;}//非静态成员变量占对象空间int mA;//静态成员变量不占对象空间static int mB; //函数也不占对象空间&#…...
计算机竞赛 深度学习卫星遥感图像检测与识别 -opencv python 目标检测
文章目录 0 前言1 课题背景2 实现效果3 Yolov5算法4 数据处理和训练5 最后 0 前言 🔥 优质竞赛项目系列,今天要分享的是 🚩 **深度学习卫星遥感图像检测与识别 ** 该项目较为新颖,适合作为竞赛课题方向,学长非常推荐…...
java web+Mysql e-life智能生活小区物业管理系统
本项目为本人自己书写,主要服务小区业主和管理人员。 e-life智能生活小区涉及多个方面的智能化和便利化服务: 1. 用户模块:包含基本的登入登出操作,查看个人信息中用户可以查看 自己的个人资料但不可以修改个人信息。 a) 用户…...
AttributeError: module ‘dgl‘ has no attribute ‘batch_hetero‘
DGLWarning: From v0.5, DGLHeteroGraph is merged into DGLGraph. You can safely replace dgl.batch_hetero with dgl.batch...
Vue项目搭建图文详解教程
版权声明 本文原创作者:谷哥的小弟作者博客地址:http://blog.csdn.net/lfdfhl 预备工作 请在本地创建文件夹用于存放Vue项目,例如:创建HelloWorld文件夹存放即将创建的Vue新项目。 创建Vue项目 首先,请在DOS中将目录…...
SpringMVC处理请求核心流程
一、前言 SpringMVC是一个基于Java的Web框架,它使用MVC(Model-View-Controller)设计模式来处理Web请求。在SpringMVC中,请求处理的核心流程主要包括以下几个步骤: 1、用户发送请求到前端控制器(Dispatche…...
SoloX:Android和iOS性能数据的实时采集工具
SoloX:Android和iOS性能数据的实时采集工具 github地址:https://github.com/smart-test-ti/SoloX 最新版本:V2.7.6 一、SoloX简介 SoloX是开源的Android/iOS性能数据的实时采集工具,目前主要功能特点: 无需ROOT/越狱…...
【知识点随笔分析 | 第五篇】简单介绍什么是QUIC
前言: 随着互联网的快速发展,传统的基于TCP的协议开始显现出一些局限性。TCP在连接建立和拥塞控制方面存在一定的延迟,这可能导致用户在访问网页、观看视频或玩网络游戏时感受到不必要的等待时间。而QUIC作为一种新兴的传输协议,试…...
vscode ssh 远程免密登录开发
存放代码的机器运行 sshd, vscode 所在机器保证可以通过 ssh 登录服务器vscode 机器通过 ssh-keygen 生成 ssh 公私钥对(已有可以忽略)将客户端的 id_rsa.pub 加入到服务器的鉴权队列 cat id_rsa.pub >> authorized_keysvscode 配置ssh登录即可.ctrlp, remote-ssh: open …...
辅助驾驶功能开发-测试篇(2)-真值系统介绍
1 真值系统概述 1.1 真值评测系统核心应用 快速构建有效感知真值,快速完成感知性能评估,快速分析感知性能缺陷。 主要应用场景包括: 1. 感知算法开发验证: 在算法开发周期中,评测结果可以作为测试报告的一部分,体现算法性能的提升。 2. 遴选供应…...
运行程序时msvcr110.dll丢失的解决方法,msvcr110.dll丢失5的个详细解决方法
在使用电脑的过程中,我们经常会遇到各种问题,其中之一就是 msvcr110.dll 丢失的问题。msvcr110.dll 是 Microsoft Visual C Redistributable 的一个组件,用于支持使用 Visual C 编写的应用程序。如果您的系统中丢失了这个文件,您可…...
已解决 Bug——IndexError: index 3 is out of bounds for axis 0 with size 3问题
🌷🍁 博主猫头虎(🐅🐾)带您 Go to New World✨🍁 🦄 博客首页: 🐅🐾猫头虎的博客🎐《面试题大全专栏》 🦕 文章图文并茂🦖…...
WEB3 solidity 带着大家编写测试代码 操作订单 创建/取消/填充操作
好 在我们的不懈努力之下 交易所中的三种订单函数已经写出来了 但是 我们只是编译 确认了 代码没什么问题 但还没有实际的测试过 这个测试做起来 其实就比较的麻烦了 首先要有两个账号 且他们都要在交易所中有存入 我们还是先将 ganache 的虚拟环境启动起来 然后 我们在项目…...
c++-vector
文章目录 前言一、vector介绍二、vector使用1、构造函数2、vector 元素访问3、vector iterator 的使用4、vector 空间增长问题5、vector 增删查改6、理解vector<vector< int >>7、电话号码的字母组合练习题 三、模拟实现vector1、查看STL库源码中怎样实现的vector2…...
十四天学会C++之第二天(函数和库)
1. 函数的定义和调用 在C中,函数是组织和结构化代码的关键工具之一。它们允许您将一段代码封装成一个可重复使用的模块,这有助于提高代码的可读性和维护性。 为什么使用函数? 函数在编程中的作用不可小觑。它们有以下几个重要用途…...
蓝桥杯每日一题2023.10.3
杨辉三角形 - 蓝桥云课 (lanqiao.cn) 题目描述 题目分析 40分写法: 可以自己手动构造一个杨辉三角,然后进行循环,用cnt记录下循环数的个数,看哪个数与要找的数一样,输出cnt #include<bits/stdc.h> using na…...
JavaScript系列从入门到精通系列第十二篇:JavaScript中对象的简介和对象的基本操作以及JavaScript中的属性值和属性名
文章目录 前言 一:对象分类 1:内建对象 2:宿主对象 3:自建对象 二:对象的基本操作 1:创建对象 2:向对象中添加属性 3:读取对象中的属性 4:修改对象中的属性 三…...
React 第五十五节 Router 中 useAsyncError的使用详解
前言 useAsyncError 是 React Router v6.4 引入的一个钩子,用于处理异步操作(如数据加载)中的错误。下面我将详细解释其用途并提供代码示例。 一、useAsyncError 用途 处理异步错误:捕获在 loader 或 action 中发生的异步错误替…...
理解 MCP 工作流:使用 Ollama 和 LangChain 构建本地 MCP 客户端
🌟 什么是 MCP? 模型控制协议 (MCP) 是一种创新的协议,旨在无缝连接 AI 模型与应用程序。 MCP 是一个开源协议,它标准化了我们的 LLM 应用程序连接所需工具和数据源并与之协作的方式。 可以把它想象成你的 AI 模型 和想要使用它…...
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...
从零实现STL哈希容器:unordered_map/unordered_set封装详解
本篇文章是对C学习的STL哈希容器自主实现部分的学习分享 希望也能为你带来些帮助~ 那咱们废话不多说,直接开始吧! 一、源码结构分析 1. SGISTL30实现剖析 // hash_set核心结构 template <class Value, class HashFcn, ...> class hash_set {ty…...
【Java_EE】Spring MVC
目录 Spring Web MVC 编辑注解 RestController RequestMapping RequestParam RequestParam RequestBody PathVariable RequestPart 参数传递 注意事项 编辑参数重命名 RequestParam 编辑编辑传递集合 RequestParam 传递JSON数据 编辑RequestBody …...
算法岗面试经验分享-大模型篇
文章目录 A 基础语言模型A.1 TransformerA.2 Bert B 大语言模型结构B.1 GPTB.2 LLamaB.3 ChatGLMB.4 Qwen C 大语言模型微调C.1 Fine-tuningC.2 Adapter-tuningC.3 Prefix-tuningC.4 P-tuningC.5 LoRA A 基础语言模型 A.1 Transformer (1)资源 论文&a…...
return this;返回的是谁
一个审批系统的示例来演示责任链模式的实现。假设公司需要处理不同金额的采购申请,不同级别的经理有不同的审批权限: // 抽象处理者:审批者 abstract class Approver {protected Approver successor; // 下一个处理者// 设置下一个处理者pub…...
安全突围:重塑内生安全体系:齐向东在2025年BCS大会的演讲
文章目录 前言第一部分:体系力量是突围之钥第一重困境是体系思想落地不畅。第二重困境是大小体系融合瓶颈。第三重困境是“小体系”运营梗阻。 第二部分:体系矛盾是突围之障一是数据孤岛的障碍。二是投入不足的障碍。三是新旧兼容难的障碍。 第三部分&am…...
uniapp手机号一键登录保姆级教程(包含前端和后端)
目录 前置条件创建uniapp项目并关联uniClound云空间开启一键登录模块并开通一键登录服务编写云函数并上传部署获取手机号流程(第一种) 前端直接调用云函数获取手机号(第三种)后台调用云函数获取手机号 错误码常见问题 前置条件 手机安装有sim卡手机开启…...
搭建DNS域名解析服务器(正向解析资源文件)
正向解析资源文件 1)准备工作 服务端及客户端都关闭安全软件 [rootlocalhost ~]# systemctl stop firewalld [rootlocalhost ~]# setenforce 0 2)服务端安装软件:bind 1.配置yum源 [rootlocalhost ~]# cat /etc/yum.repos.d/base.repo [Base…...
