当前位置:网站首页>Day24: file system
Day24: file system
2022-07-04 21:15:00 【_ Doris___】
Catalog
One 、firesystem brief introduction :
1.C++17 Standard only , In fact, it's about folders 、 File directory
2.filesystem brief introduction :
4. Common methods :( notes : stay filesystem In the namespace of filesystem::)
(3) Get the time when the file was last modified
How to convert std::filesystem::file_time_type to time_t?
Two 、 Three categories path class
1. Determine whether this path exists
2. Get path related properties :
3、 ... and 、 Three categories file_status class
1. Create file_status object : utilize filesystem::status(string url)
2. Methods in class :type() Returns one of several enumeration types (filesystem::file_type Medium )
Four 、 Three categories file_status class
1. Traverse all files in the current directory
2. Traverse all files in the current folder ---> Only the current directory
3. Traverse all folder files under the current folder
4. Delete all files in the current path ( Folder not included !)
One 、firesystem brief introduction :
1.C++17 Standard only , In fact, it's about folders 、 File directory
#include<filesystem>
2.filesystem brief introduction :
3. Three broad categories :
①path class ②file_status class ③directory_entry class
4. Common methods :( notes : stay filesystem In the namespace of filesystem::)
(1) establish
①create_directory(string url)
The second parameter is a error_code object , By calling error_code Medium message() Method , Whether the output is successfully created . secondly , You cannot create a parent folder and create subfolders in it ( Only single tier directories can be created )
②create_directories(string url) Can create multistage Catalog
notes : If the path already exists , It will not be cleared .
filesystem::path url("fileBox");
if (!filesystem::exists(url))
{
cout << " non-existent " << endl;
}/* Be sure to give priority to detection */
// Path storage Do nothing else
filesystem::create_directory("fileBox"); // Create a single level directory
error_code temp;
filesystem::create_directory("fileBox/xx",temp);
cout << temp.message() << endl;
filesystem::create_directories("a/b/c",temp); // Create multi-level directory
cout << temp.message() << endl;
(2) Delete path
remove_all(string url)
filesystem::remove_all(url);
(3) Get the time when the file was last modified
last_write_time(string url)
Return to one file_time_type type , Need further adjustment time_since_epoch() Method .count() Get the form of timestamp
Last class mainly talked about two ways : Method of converting timestamp into readable time .
①ctime(&m_tm)
②std::tm* p=localtime(&m_tm);
cout << " Format time :" << put_time(p, "%F %T") << endl;
notes : Later, through the following two lines of code to detect the data type ,count() It is not time_t type , So the above two methods are not very effective .
#include<typeinfo>
typeid(m_time).name()
Start with the essence : The problem is :(from stack_overflow)
How to convert std::filesystem::file_time_type to time_t?
C++20 Of solution:
const auto fileTime = std::filesystem::last_write_time(filePath);
const auto systemTime = std::chrono::clock_cast<std::chrono::system_clock>(fileTime);
const auto time = std::chrono::system_clock::to_time_t(systemTime);
So the example code is changed to :
auto filetime = filesystem::last_write_time("B");
const auto systemTime =
std::chrono::clock_cast<std::chrono::system_clock>(filetime);
const auto time = std::chrono::system_clock::to_time_t(systemTime);
std::tm* p = localtime(&time);
cout << " File change time :" << put_time(p,"%F %T") << endl;
Two 、 Three categories path class
1. Determine whether this path exists
notes : It must be filesystem Scope discriminator in bool filesystem::exist(string url)
#include<iostream>
#include<filesystem>
filesystem::path url("file");
if (!filesystem::exists(url))
{
cout << " path does not exist " << endl;
}
2. Get path related properties :
(i) Get the current path (ii) Get the root directory (iii) Relative paths (iv) Get root name (v) Get the root path
filesystem::path curURL = filesystem::current_path();
//C:\Users\team\Desktop\ The first 24 course C++ file system \filesystem System \path class
cout << curURL.string() << endl; // Double slash becomes single slash
cout <<" current path :" << curURL << endl; // Double slash
cout << " root directory :" << curURL.root_directory() << endl;
cout << " Relative paths :" << curURL.relative_path() << endl;
cout << " Root name :" << curURL.root_name() << endl;
cout << " The root path :" << curURL.root_path() << endl;
Output :
D:\C++\C++ file system \filesystem System \path class
current path :"D:\\C++\\C++ file system \\filesystem System \\path class "
root directory :"\\"
Relative paths :"C++\\C++ file system \\filesystem System \\path class "
Root name :"D:"
The root path :"D:\\"
3、 ... and 、 Three categories file_status class
1. Create file_status object : utilize filesystem::status(string url)
2. Methods in class :type() Returns one of several enumeration types (filesystem::file_type Medium )
#include <filesystem>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test_file_status(filesystem::file_status x)
{
switch (x.type())
{
case filesystem::file_type::regular:
cout << " Disk files " << endl;
break;
case filesystem::file_type::directory:
cout << " Directory file " << endl;
break;
case filesystem::file_type::not_found:
cout << " directory does not exist " << endl;
break;
case filesystem::file_type::unknown:
cout << " Unrecognized file " << endl;
break;
}
}
int main()
{
filesystem::create_directory("file");
filesystem::file_status x = filesystem::status("file");
test_file_status(x);
fstream file("file\\test.dat", ios::out | ios::trunc);
if (!file)
{
cout << " File creation failed !" << endl;
}
test_file_status(filesystem::status("file\\test.dat"));
return 0;
}
Output :
Directory file
Disk files
Four 、 Three categories file_status class
notes : If you can't recognize filesystem It must be that the standard is not adjusted c++17 above !
directory_entry: File entry
dirctory_iterator: Traversal filerecursive_directory_iterator Traverse all ( With recursive traversal of all folders !!!)
1. Traverse all files in the current directory
// Traverse all files in the current directory :
void traverseDirectory()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << " The current path does not exist " << endl;
return;
}
filesystem::directory_entry input(url);// entrance
if (input.status().type() != filesystem::file_type::directory)
{
cout << "url Not a directory " << endl;
return;
}
filesystem::directory_iterator dir(url);
for (auto v : dir)
{/*path Is a path object */
cout << v.path().filename() << endl;
}
}
2. Traverse all files in the current folder ---> Only the current directory
// Traverse all files in the current folder ---> Only the current directory
void traverseDirectoryAllFile()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << " The current path does not exist " << endl;
return;
}
set<string> dirset;
filesystem::directory_iterator begin(url);
for (filesystem::directory_iterator end; begin != end; ++begin)
{
if (!filesystem::is_directory(begin->path()))
{
dirset.insert(begin->path().filename().string());
}
}
for (auto v : dirset)
{
cout << v << endl;
}
}
3. Traverse all folder files under the current folder
recursive_directory_iterator Traverse all ( With recursive traversal of all folders !!!)
// Traverse all folder files under the current folder
void traverseAllDirectoryAllFile()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << " The current path does not exist " << endl;
return;
}
set<string> dirset;
//recursive_directory_iterator Traverse all ( With recursive traversal of all folders !!!)
filesystem::recursive_directory_iterator begin(url);
for (filesystem::recursive_directory_iterator end; begin != end; ++begin)
{
if (!filesystem::is_directory(begin->path()))
{
dirset.insert(begin->path().filename().string());
}
}
for (auto v : dirset)
{
cout << v << endl;
}
}
4. Delete all files in the current path ( Folder not included !)
// Delete all files in the current path ( Folder not included !)
void deleteURLAllFile()
{
filesystem::path root = filesystem::current_path();
set<string> dirset;
for (filesystem::directory_iterator end, begin(root); begin != end; ++begin)
{
if (!filesystem::is_directory(begin->path()))/* Exclude folders */
{
dirset.insert(begin->path().filename().string());
}
}
for (auto v : dirset)
{
if(v!="test.exe")/* The program cannot put itself .exe Delete the file , So we need to rule out */
filesystem::remove_all(v); // heavy load /
}
}
边栏推荐
- 2021 CCPC 哈尔滨 I. Power and Zero(二进制 + 思维)
- [solution] paddlepaddle 2 X call static graph mode
- LeetCode 8. 字符串转换整数 (atoi)
- Huawei ENSP simulator configures ACL access control list
- 杰理之AD 系列 MIDI 功能说明【篇】
- MySQL --- 数据库查询 - 聚合函数的使用、聚合查询、分组查询
- Go notes (1) go language introduction and characteristics
- Leetcode+ 81 - 85 monotone stack topic
- [observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
- 接口设计时的一些建议
猜你喜欢
Hwinfo hardware detection tool v7.26 green version
PS vertical English and digital text how to change direction (vertical display)
Configuration of DNS server of Huawei ENSP simulator
华为ensp模拟器实现通信安全(交换机)
Idea configuration standard notes
杰理之增加进关机前把触摸模块关闭流程【篇】
仿ps样式js网页涂鸦板插件
NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
【1200. 最小绝对差】
Pytorch---使用Pytorch实现LinkNet进行语义分割
随机推荐
B站视频 声音很小——解决办法
Ten years' experience of byte test engineer directly hits the pain point of UI automation test
js 3D爆炸碎片图片切换js特效
杰理之AD 系列 MIDI 功能说明【篇】
PS竖排英文和数字文字怎么改变方向(变竖直显示)
NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??
FastDfs的快速入门,三分钟带你上传下载文件到云服务器
【观察】联想:3X(1+N)智慧办公解决方案,释放办公生产力“乘数效应”
多模输入事件分发机制详解
Why is TCP three handshakes and four waves
测试用例 (TC)
Go notes (1) go language introduction and characteristics
杰理之AD 系列 MIDI 功能说明【篇】
Go language notes (4) go common management commands
heatmap.js图片热点热力图插件
福昕PDF编辑器v10.1.8绿色版
Idea configuration standard notes
Idea case shortcut
acwing 3302. Expression evaluation
MySQL statement execution details