目录
1.C++17标准才有的,其实就是对文件夹、文件目录进行操作
4.常用方法:(注:在filesystem的命名空间中 filesystem::)
How to convert std::filesystem::file_time_type to time_t?
1.创建出file_status对象:利用filesystem::status(string url)
2.类中的方法:type()返回几种枚举类型之一(filesystem::file_type中的)
一、firesystem简介:
1.C++17标准才有的,其实就是对文件夹、文件目录进行操作
#include<filesystem>
2.filesystem简介:
3.三大类:
①path类②file_status类 ③directory_entry类
4.常用方法:(注:在filesystem的命名空间中 filesystem::)
(1)创建
①create_directory(string url)
第二个参数是一个error_code对象,通过调用error_code中的message()方法,即可进行输出是否创建成功。其次,不能连着创建一个母文件夹并在其中创建子文件夹(只能创建单层目录)
②create_directories(string url) 可创建多级目录
注:若该路径已经存在了,是不会进行清除操作的。
filesystem::path url("fileBox");
if (!filesystem::exists(url))
{
cout << "不存在" << endl;
}/*一定要优先检测*/
//路径存储 不做其他操作
filesystem::create_directory("fileBox"); //创建单层目录
error_code temp;
filesystem::create_directory("fileBox/xx",temp);
cout << temp.message() << endl;
filesystem::create_directories("a/b/c",temp); //创建多级目录
cout << temp.message() << endl;
(2)删除路径
remove_all(string url)
filesystem::remove_all(url);
(3)获取最后一次修改文件的时间
last_write_time(string url)
返回一个file_time_type类型,需要进一步调佣time_since_epoch()方法.count()获取时间戳的形式
上节课主要讲了两种方式:将时间戳转化为可读时间的方法。
①ctime(&m_tm)
②std::tm* p=localtime(&m_tm);
cout << "格式化时间:" << put_time(p, "%F %T") << endl;
注:后来通过下面两行检测数据类型的代码发现,count()返回的并不是time_t类型,所以上述两种方法不太奏效。
#include<typeinfo>
typeid(m_time).name()
从本质入手:问题即:(from stack_overflow)
How to convert std::filesystem::file_time_type to time_t?
C++20的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);
所以实例代码改为:
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 << "文件改动时间:" << put_time(p,"%F %T") << endl;
二、三大类之path类
1.判断此路径是否存在
注:一定是 filesystem中的作用域分辨符 bool filesystem::exist(string url)
#include<iostream>
#include<filesystem>
filesystem::path url("file");
if (!filesystem::exists(url))
{
cout << "路径不存在" << endl;
}
2.获取路径相关的属性:
(i)获取当前路径 (ii)获取根目录 (iii)相对路径 (iv)获取根名 (v)获取根路径
filesystem::path curURL = filesystem::current_path();
//C:\Users\team\Desktop\第24课 C++文件系统\filesystem系统\path类
cout << curURL.string() << endl; //双斜杠变为单斜杠
cout <<"当前路径:" << curURL << endl; //双斜杠
cout << "根目录:" << curURL.root_directory() << endl;
cout << "相对路径:" << curURL.relative_path() << endl;
cout << "根名:" << curURL.root_name() << endl;
cout << "根路径:" << curURL.root_path() << endl;
输出:
D:\C++\C++文件系统\filesystem系统\path类
当前路径:"D:\\C++\\C++文件系统\\filesystem系统\\path类"
根目录:"\\"
相对路径:"C++\\C++文件系统\\filesystem系统\\path类"
根名:"D:"
根路径:"D:\\"
三、三大类之file_status类
1.创建出file_status对象:利用filesystem::status(string url)
2.类中的方法:type()返回几种枚举类型之一(filesystem::file_type中的)
#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 << "磁盘文件" << endl;
break;
case filesystem::file_type::directory:
cout << "目录文件" << endl;
break;
case filesystem::file_type::not_found:
cout << "目录不存在" << endl;
break;
case filesystem::file_type::unknown:
cout << "无法识别文件" << 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 << "文件创建失败!" << endl;
}
test_file_status(filesystem::status("file\\test.dat"));
return 0;
}
输出:
目录文件
磁盘文件
四、三大类之file_status类
注:若识别不出filesystem一定是标准没调到c++17以上!
directory_entry:文件入口
dirctory_iterator: 遍历文件recursive_directory_iterator 遍历所有的(自带递归遍历所有的文件夹!!!)
1.遍历当前目录下所有文件
//遍历当前目录下所有文件:
void traverseDirectory()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << "当前路径不存在" << endl;
return;
}
filesystem::directory_entry input(url);//入口
if (input.status().type() != filesystem::file_type::directory)
{
cout << "url不是目录" << endl;
return;
}
filesystem::directory_iterator dir(url);
for (auto v : dir)
{/*path是一个路径对象*/
cout << v.path().filename() << endl;
}
}
2.遍历当前文件夹中所有文件--->只限当前目录
//遍历当前文件夹中所有文件--->只限当前目录
void traverseDirectoryAllFile()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << "当前路径不存在" << 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.遍历当前文件夹下所有文件夹文件
recursive_directory_iterator 遍历所有的(自带递归遍历所有的文件夹!!!)
//遍历当前文件夹下所有文件夹文件
void traverseAllDirectoryAllFile()
{
filesystem::path url("D:\\BaiduNetdiskDownload");
if (!filesystem::exists(url))
{
cout << "当前路径不存在" << endl;
return;
}
set<string> dirset;
//recursive_directory_iterator 遍历所有的(自带递归遍历所有的文件夹!!!)
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.删除当前路径下的所有文件(不包含文件夹!)
//删除当前路径下的所有文件(不包含文件夹!)
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()))/*排除掉文件夹*/
{
dirset.insert(begin->path().filename().string());
}
}
for (auto v : dirset)
{
if(v!="test.exe")/*程序不能把自己.exe文件删除掉,所以要排除一下*/
filesystem::remove_all(v); //重载/
}
}