编程语言
首页 > 编程语言> > C++文件系统

C++文件系统

作者:互联网

filesystem简介

#include <filesystem>
#include <iostream>
using namespace std;

int main()
{
	filesystem::path ur1("fileBox");
	if (!filesystem::exists(ur1))
	{
		cout << "不存在" << endl;
	}//路径存在不做创建处理
	filesystem::create_directory("fileBox");
	//如果目录下没有就创建一个叫fileBox的文件夹,单层目录
	filesystem::create_directories("a/b/c");//创建多级目录
	filesystem::remove_all(ur1);//删除整个文件夹
	auto time = filesystem::last_write_time("a/b/c");//得到c最后一次写的时间
	cout << time.time_since_epoch() << endl;
	return 0;
}

在官方文档还有好多

path类

 filesystem::path ur1("fileBox");
	filesystem::path curUAL = filesystem::current_path();
	cout << curUAL.string() << endl;//输出当前文件路径
	cout << curUAL.root_directory() << endl;//根目录
	cout << curUAL.relative_path() << endl;//相对路径
	cout << curUAL.root_name() << endl;//根名
	cout << curUAL.root_path() << endl;//根路径

file_statues类

文件状态判断:

void test_file_statues(filesystem::file_status a)
{
	switch (a.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::none:
		cout << "不清楚" << endl;
	}
}


 filesystem::create_directory("file");
	 test_file_statues(filesystem::status("file"));

遍历文件操作

void traverDirectory()
{
	filesystem::path ur1("C:\\Users");
	if (!filesystem::exists(ur1))
		cout << "路径不存在" << endl;
	filesystem::directory_entry input(ur1);
	if (input.status().type() != filesystem::file_type::directory)
		cout << "ur1不是目录" << endl;
	filesystem::directory_iterator dir(ur1);
	for (auto v : dir)
		cout << v.path().filename() << endl;

}
//遍历当前文件夹下的所有文件
void traverDirectoryAllfile()
{
	filesystem::path ur1("C:\\");
	if (!filesystem::exists(ur1))
		cout << "路径不存在" << endl;
	set<string> dirset;
	filesystem::directory_iterator begin(ur1);
	for (filesystem::directory_iterator end, begin(ur1); begin != end; ++begin)
	{
		if (!filesystem::is_directory(begin->path()))
		{
			dirset.insert(begin->path().filename().string());
		}
	}
	for (auto v : dirset)
	{
		cout << v << endl;
	}

}

标签:begin,cout,文件系统,C++,ur1,file,filesystem,path
来源: https://blog.csdn.net/qq_50208608/article/details/122383779