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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/92421.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

EasyExcel的源码流程(导入Excel)

1. 入口 2. EasyExcel类继承了EasyExcelFactory类&#xff0c;EasyExcel自动拥有EasyExcelFactory父类的所有方法&#xff0c;如read()&#xff0c;readSheet()&#xff0c;write()&#xff0c;writerSheet()等等。 3. 进入.read()方法&#xff0c;需要传入三个参数(文件路径…

【C++】:类和对象(1)

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关C中类和对象的知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到精通…

什么是序列化和反序列化?

什么是序列化和反序列化&#xff1f; 什么是序列化和反序列化&#xff1f; 序列化和反序列化是计算机科学中两个重要的概念&#xff0c;主要应用在数据存储和网络传输等场景。序列化是将数据结构或对象状态转换为可以存储或传输的形式的过程。这种形式要求在重新构建原始对象…

Windows电脑显示部分功能被组织控制

目录 问题描述 解决方法 总结 问题描述 如果你的电脑出现以上情况&#xff0c;建议你使用我这种方法&#xff08;万不得已&#xff09; 解决方法 原因就是因为当时你的电脑在激活的时候是选择了组织性激活的&#xff0c;所以才会不管怎么搞&#xff0c;都无法摆脱组织的控…

十五、异常(4)

本章概要 Java 标志异常 特例&#xff1a;RuntimeException 使用 finally 进行清理 finally 用来做什么&#xff1f;在 return 中使用 finally缺憾&#xff1a;异常丢失 Java 标准异常 Throwable 这个 Java 类被用来表示任何可以作为异常被抛出的类。Throwable 对象可分为两…

C/C++笔试面试真题

C/C++笔试面试真题 1、堆和栈的区别 1、栈由系统自动分配,而堆是人为申请开辟; 2、栈获得的空间较小,而堆获得的空间较大; 3、栈由系统自动分配,速度较快,而堆一般速度比较慢; 4、栈是连续的空间,而堆是不连续的空间。 2、什么是野指针?产生的的原因? 野指针的指向的…

ubuntu下源码编译方式安装opencv

基础条件 ubuntu 20.04 opencv 3.4.3 opencv 源码编译的安装步骤 第一步&#xff0c; 首先clone源码 git clone https://github.com/opencv/opencv.git第二步&#xff0c;依赖包&#xff0c;执行下面的命令 sudo apt-get install build-essential sudo apt-get install cmak…

记一次Mybatis驼峰命名导致的线上BUG及处理方案

前言 方向从一开始就错了&#xff0c;还是执着的去寻找问题的解决方案&#xff0c;简直就是一场重大灾难&#xff0c;但这也是每个修行者的必由之路。这个线上问题&#xff0c;差点让我的心里防线崩溃&#xff0c;苦寻无门&#xff0c;最终得以解决也多亏了身边的各路大佬的群…

Android studio “Layout Inspector“工具在Android14 userdebug设备无法正常使用

背景描述 做rom开发的都知道&#xff0c;“Layout Inspector”和“Attach Debugger to Android Process”是studio里很好用的工具&#xff0c;可以用来查看布局、调试系统进程&#xff08;比如setting、launcher、systemui&#xff09;。 问题描述 最进刚开始一个Android 14…

数据结构与算法之堆: Leetcode 215. 数组中的第K个最大元素 (Typescript版)

数组中的第K个最大元素 https://leetcode.cn/problems/kth-largest-element-in-an-array/ 描述 给定整数数组 nums 和整数 k&#xff0c;请返回数组中第 k 个最大的元素。请注意&#xff0c;你需要找的是数组排序后的第 k 个最大的元素&#xff0c;而不是第 k 个不同的元素。…

Android Shape设置背景

设置背景时&#xff0c;经常这样 android:background“drawable/xxx” 。如果是纯色图片&#xff0c;可以考虑用 shape 替代。 shape 相比图片&#xff0c;减少资源占用&#xff0c;缩减APK体积。 开始使用。 <?xml version"1.0" encoding"utf-8"?…

云安全之HTTP协议介绍

HTTP的基本概念 什么是网络协议 网络协议是计算机之间为了实现网络通信而达成的一种“约定”或者”规则“&#xff0c;有了这种”约定不同厂商生产的设备&#xff0c;以及不同操作系统组成的计算机之间&#xff0c;就可以实现通信。 网络协议由三个要素构成&#xff1a;1、语…

WSL2和ubuntu的安装过程

目录 1.WSL2的安装 2.Ubuntu的安装 3.安装完成后的打开方式 1.WSL2的安装 按下WINX键&#xff0c;选择Windows PowerShell (管理员) 1.1执行以下命令&#xff0c;该命令的作用是&#xff1a;启用适用于 Linux 的 Windows 子系统 dism.exe /online /enable-feature /featur…

Oracle 快速入门

当你刚开始探索 Oracle 数据库时&#xff0c;可能会觉得有些复杂。然而&#xff0c;本文将为你提供 Oracle 数据库的快速入门指南&#xff0c;帮助你迅速上手这个强大的关系型数据库管理系统&#xff08;RDBMS&#xff09;。无论你是数据库新手还是有经验的数据库管理员&#x…

【小沐学前端】Node.js实现基于Protobuf协议的WebSocket通信

文章目录 1、简介1.1 Node1.2 WebSocket1.3 Protobuf 2、安装2.1 Node2.2 WebSocket2.2.1 nodejs-websocket2.2.2 ws 2.3 Protobuf 3、代码测试3.1 例子1&#xff1a;websocket&#xff08;html&#xff09;3.1.1 客户端&#xff1a;yxy_wsclient1.html3.1.2 客户端&#xff1a…

【踩坑日记】Docker elasticsearch too many open files问题处理

项目场景&#xff1a; 使用单机ES作为日志存储数据库&#xff0c;每日生成一个日期索引&#xff0c;由于每日的数据量可能较大&#xff0c;有时候需要进行磁盘扩容操作&#xff0c;本次问题记录还未找到根本的触发原因&#xff0c;后续找到原因后再进行记录 问题描述 每日创建…

利用mAP计算yolo精确度

当将yolo算法移植部署在嵌入式设备上&#xff0c;为了验证算法的准确率。将模型测试的结果保存为txt文件&#xff08;每一个txt文件&#xff0c;对应一个图片&#xff09;。此外&#xff0c;需要将数据集中的标签由[x,y,w,h]转为[x1,y1,x2,y2]。最后&#xff0c;运行验证代码 …

【redisson学习笔记】

1)clone项目 git clone https://github.com/redisson/redisson.git本来想直接用maven编译源码&#xff0c; 却发现各种错误&#xff0c;主要是maven的编译插件版本问题。 2)然后用maven包方式引入 <dependencies><dependency><groupId>org.redisson</gr…

Secureboot从入门到精通

关键词&#xff1a;trustzone视频、tee视频、ATF视频、secureboot视频、安全启动视频、selinux视频&#xff0c;cache视频、mmu视频&#xff0c;armv8视频、armv9视频 FF-A视频、密码学视频、RME/CCA视频、学习资料下载、免费学习资料、免费 周贺贺&#xff0c;baron&#xff0…

把握现在,热爱生活

博客主页&#xff1a;https://tomcat.blog.csdn.net 博主昵称&#xff1a;农民工老王 主要领域&#xff1a;Java、Linux、K8S 期待大家的关注&#x1f496;点赞&#x1f44d;收藏⭐留言&#x1f4ac; 目录 厨艺房价琐事计划随想 今年的中秋国庆假期放8天&#xff0c;比春节假期…