filesystem
#include <filesystem>
#include <iostream>
using namespace std;
int main()
{
//打开文件夹
filesystem::path url("file");
//判断当前文件夹是否存在
if (!filesystem::exists(url))
{
cout << "相对路径文件夹不存在" << endl;
}
//创建单层文件夹,路径存在不做其他操作
filesystem::create_directory("file");
//存储错误信息
error_code err;
//将文件夹是否创建成功的信息存入
filesystem::create_directory("file/x", err);
//将存储的错误信息打印出来
cout << err.message() << endl;
//创建多层文件夹
filesystem::create_directories("sss/ss/s", err);
cout << err.message() << endl;
//删除根文件夹
filesystem::remove_all(url);
//获取文件最后一次的操作时间
auto time = filesystem::last_write_time("sss");
//获取时间戳
cout << time.time_since_epoch().count() << endl;
//获取当前路径
filesystem::path CUR = filesystem::current_path();
cout << CUR<< endl;
cout << CUR.root_directory() << endl;//根目录
cout << CUR.relative_path() << endl;//相对路径
cout << CUR.root_name() << endl;//根名
cout << CUR.root_path() << endl;//根路径
return 0;
}
文件状态的判断
#include <filesystem>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
void test(filesystem::file_status temp)
{
switch (temp.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");
test(filesystem::status("file"));//目录文件
fstream File("file\\test.dat", ios::out | ios::trunc);
if (!File)
{
cout << "创建失败" << endl;
}
test(filesystem::status("file\\test.dat"));//磁盘文件
return 0;
}
遍历删除文件
#include <filesystem>
#include <iostream>
#include <string>
#include <set>
using namespace std;
//遍历当前目录下所有文件
void traverseDirectory()
{
filesystem::path url("D:\\");
if (!filesystem::exists(url))
{
cout << "当前文件不存在" << endl;
return ;
}
//选定文件路口
filesystem::directory_entry input_url(url);
if (input_url.status().type() != filesystem::file_type::directory)
{
cout << "url不是目录" << endl;
}
else
{
filesystem::directory_iterator Iter(url);
for (auto v : Iter)
{
cout << v.path() << endl;
}
}
}
//遍历当前文件夹下所有文件
void traverseDirectoryAllfile()
{
filesystem::path url("D:\\vs2022\\Common7");
if (!filesystem::exists(url))
{
cout << "当前文件不存在" << endl;
return;
}
set<string> dirset;
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;
}
}
//删除
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!="cpp实验场.exe")
filesystem::remove_all(v);
}
}
int main()
{
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)