c++17文件系统<filesystem>
// : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include ""
#include <iostream>
#include <filesystem>
#include <>
using namespace std;
namespace fs = std::experimental::filesystem;
// 计算文件大小
uintmax_t ComputeFileSize(const fs::path& pathToCheck)
{
if (fs::exists(pathToCheck) && fs::is_regular_file(pathToCheck))
{
auto err = std::error_code{};
auto filesize = fs::file_size(pathToCheck, err);
if (filesize != static_cast<uintmax_t>(-1))
{
return filesize;
}
}
return static_cast<uintmax_t>(-1);
}
// 遍历文件夹找出所有文件(非递归)
void FindAllFile(std::string path)
{
int count = 0;
for (auto& v : fs::directory_iterator(path))
{
std::string filename = v.path().filename().string();
//std::cout << "type is:" << typeid(v).name();
//判断是否正常的文件,如果遍历到文件夹,则可判断出不是正常的文件
if (fs::is_regular_file(v.path()))
{
//std::cout << " is regular_file" << std::endl;
std::cout << filename << "\n";;
//std::string extension_name = ().extension().string();
//std::cout << extension_name << std::endl;
}
else
{
//std::cout << " not regular_file" << std::endl;
}
}
}
// 遍历文件夹找出所有文件(递归)
void FindTreeFile(std::string path)
{
for (auto& v : fs::recursive_directory_iterator(path))
{
if (fs::is_regular_file(v.path()))
{
std::string filename = v.path().filename().string();
std::cout << filename << std::endl;
//std::string extension_name = ().extension().string();
//std::cout << extension_name << std::endl;
}
}
}
int main()
{
// 获取路径的不同部分
fs::path pathToShow("C:\\Windows\\");
cout << "获取路径的不同部分:" << "\n";
cout << "exists() = " << fs::exists(pathToShow) << "\n"
<< "root_name() = " << pathToShow.root_name() << "\n"
<< "root_path() = " << pathToShow.root_path() << "\n"
<< "relative_path() = " << pathToShow.relative_path() << "\n"
<< "parent_path() = " << pathToShow.parent_path() << "\n"
<< "filename() = " << pathToShow.filename() << "\n"
<< "stem() = " << pathToShow.stem() << "\n"
<< "extension() = " << pathToShow.extension() << "\n";
cout << endl;
// 创建多级路径文件
cout << "创建多级路径文件:" << "\n";
std::string outputPath("F:\\test\\");
std::filesystem::path p(outputPath);
//p.parent_path()会返回文件路径中的文件夹路径。
std::filesystem::create_directories(p.parent_path());
cout << endl;
// 遍历path
cout << "遍历path:" << "\n";
unsigned int i = 0;
for (const auto& part : pathToShow)
{
cout << "path part: " << i++ << " = " << part << "\n";
}
cout << endl;
// 拼接path
cout << "拼接path:" << "\n";
fs::path filepath("C:\\temp");
filepath /= "user";
filepath /= "data";
cout << filepath << "\n";
//fs::path p2("C:\\temp\\");
//p2 += "user\\";
//p2 += "data";
//cout << p2 << "\n";
cout << endl;
// 计算文件大小,文件创建修改时间
cout << "计算文件大小:" << "\n";
fs::path pathToCheck("F:\\9培训.rar");
uintmax_t nLength = ComputeFileSize(pathToCheck);
cout << "file length: " << nLength << "\n";
//处理std::asctime编译错误,右击工程-->属性-->配置属性 --> C/C++ --> 命令行-->输入"/D _CRT_SECURE_NO_WARNINGS"-->"确定"
cout << "文件创建修改时间:" << "\n";
auto timeEntry = fs::last_write_time(pathToCheck);
time_t cftime = chrono::system_clock::to_time_t(timeEntry);
cout << std::asctime(std::localtime(&cftime)) << "\n";
cout << endl;
// 获取文件夹内的文件(非递归)
std::string dirPath = "F:\\123";
cout << "获取文件夹下的文件:" << "\n";
FindAllFile(dirPath);
cout << "递归获取文件夹下所有的文件:" << "\n";
FindTreeFile(dirPath);
}