Day24:文件系统

目录

一、firesystem简介:

        1.C++17标准才有的,其实就是对文件夹、文件目录进行操作

        2.filesystem简介:

        3.三大类:

        4.常用方法:(注:在filesystem的命名空间中 filesystem::)

                (1)创建

                (2)删除路径

                (3)获取最后一次修改文件的时间

How to convert std::filesystem::file_time_type to time_t?

二、三大类之path类

        1.判断此路径是否存在

         2.获取路径相关的属性:

三、三大类之file_status类

        1.创建出file_status对象:利用filesystem::status(string url)

        2.类中的方法:type()返回几种枚举类型之一(filesystem::file_type中的)

四、三大类之file_status类

1.遍历当前目录下所有文件 

2.遍历当前文件夹中所有文件--->只限当前目录

3.遍历当前文件夹下所有文件夹文件

4.删除当前路径下的所有文件(不包含文件夹!)


一、firesystem简介:

        1.C++17标准才有的,其实就是对文件夹、文件目录进行操作

#include<filesystem>

        2.filesystem简介:

 函数 | Microsoft Docshttps://docs.microsoft.com/zh-cn/cpp/standard-library/filesystem-functions?view=msvc-170#copy_file

        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);	//重载/
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Ocean__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值