一 头文件及命令空间
#include<filesystem>
using namespace std::filesystem;
二 常用类
1、path 类:说白了该类只是对字符串(路径)进行一些处理,这也是文件系统的基石。
2、directory_entry 类:功如其名,文件入口,这个类才真正接触文件。
3、directory_iterator 类:获取文件系统目录中文件的迭代器容器,其元素为 directory_entry对象(可用于遍历目录)
4、file_status 类:用于获取和修改文件(或目录)的属性(需要了解C++11的强枚举类型(即枚举类))
path p("E:/CrowdDetection/revaluate/APMRToolkits");
std::cout<<"根目录:"<<p.root_directory() << std::endl;
std::cout << "根目录:" << p.root_name() << std::endl;
std::cout << "根目录:" << p.root_path() << std::endl;
std::cout << "不带扩展的文件名:" << p.stem() << std::endl;
std::cout << "扩展名:" << p.extension() << std::endl;
std::cout << "文件名:" << p.filename() << std::endl;
std::cout << "相对目录:" << p.relative_path() << std::endl;
std::cout << "父目录:" << p.parent_path()<< std::endl;
p.replace_extension("jpg");
p.replace_filename("hahahah");
三 使用方法
#include <iostream>
#include<filesystem>
using namespace std;
using namespace std::filesystem;
int main(){
path str("C:\\Windows");
if (!exists(str)) //必须先检测目录是否存在才能使用文件入口.
return 1;
directory_entry entry(str); //文件入口
if (entry.status().type() == file_type::directory) //这里用了C++11的强枚举类型
cout << "该路径是一个目录" << endl;
directory_iterator list(str); //文件入口容器
for (auto& it:list)
cout << it.path().filename()<< endl; //通过文件入口(it)获取path对象,再得到path对象的文件名,将之输出
system("pause");
return 0;
}
四 常用库函数
void copy(const path& from, const path& to) : //目录复制
path absolute(const path& pval, const path& base = current_path()) //获取相对于base的绝对路径
bool create_directory(const path& pval) //当目录不存在时创建目录
bool create_directories(const path& pval) //形如/a/b/c这样的,如果都不存在,创建目录结构
bool exists(const path& pval) //用于判断path是否存在
uintmax_t file_size(const path& pval) //返回目录的大小
file_time_type last_write_time(const path& pval) //返回目录最后修改日期的file_time_type对象
bool remove(const path& pval) //删除目录
uintmax_t remove_all(const path& pval) //递归删除目录下所有文件,返回被成功删除的文件个数
void rename(const path& from, const path& to) //移动文件或者重命名
五总结
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <filesystem>
#include <chrono>
using namespace std;
namespace fs = std::filesystem;
/*
1\ filesystem 有三个类 path directory_entry directory_iterator file_stauts
filesystem 以path 为基础,后面所有的操作都是以path开始
2\path:方法有:判断是否存在 exists(str) 相当于字符串的操作
主要包含了 路径的拼接\绝对路径 相对路径 文件名 文件扩展名
3\ file_stauts 的使用
fs::file_stauts pp = fs::stauts(dir); // dir 可以是fs::path类型,也可以是string类型
4 directory_entry 和 directory_iteration
*/
// 1 path
void test01()
{
//1.1 创建
string SrcPath = "E:/c/hahah.txt";
fs::path pth(SrcPath);
fs::path pth2(SrcPath);
//1.2 是否存在
if (!fs::exists(pth))
{
std::cout<< "ggg" << std::endl;
fs::create_directories(pth); //如果不存在就创建目录 /a/b/c/
}
//1.3 绝对路径 路径拼接 获取文件名 扩展名 path转string
fs::path abPath = fs::absolute(pth);
fs::path filename = pth.filename();
fs::path extension = pth.extension();
fs::path stem = pth.stem();
std::string p = pth.string();
fs::path ParentPath = pth.parent_path();
std::cout << "parant dir: " << ParentPath << std::endl;
fs::path newPath = ParentPath.append("/M");
std::cout << "new dir:" << newPath << std::endl;
// 1.4 文件删除 移动 复制 remove move copy rename
fs::copy(pth,pth2);
bool res = fs::remove(pth);
}
//2 file_stauts
void test02()
{
string strDir = "E:/c/hahah.txt";
fs::path pth(strDir);
fs::file_status fileObj = fs::status(pth);
switch (fileObj.type())
{
case fs::file_type::regular:
std::cout<<"文件类型为磁盘一般文件。。。"<<std::endl;
case fs::file_type::directory:
std::cout <<"是文件夹" << std::endl;
case fs::file_type::not_found:
std::cout<<"不存在该文件" << std::endl;
case fs::file_type::unknown:
std::cout<<"无法识别的文件" << std::endl;
default:
break;
}
}
//3 directory_entry directory_iteration
void test03()
{
std::vector<std::string> files;
//3.1 创建path类型
string testDir = "E:/ppp/myclass";
fs::path pth(testDir);
//3.2 判断是否存在
if (!fs::exists(pth))
{
std::cout<<testDir<<",不存在" << std::endl;
}
if (!fs::is_directory(pth))
{
std::cout<<testDir<<",不是文件夹" << std::endl;
}
fs::directory_entry Dir(pth);
/*
//方法一
fs::directory_iterator it(Dir);
for (auto v:it)
{
if (!fs::is_directory(v))
{
std::cout << v << std::endl;
}
else
{
std::cout<<"wenjianjia:"<<v<<std::endl;
}
}
*/
//方法二
for (fs::directory_iterator end,begin(Dir);begin!=end;++begin)
{
if (!fs::is_directory(begin->path()))
{
files.push_back(begin->path().string());
}
}
}