C++17文件系统库通过std::filesystem::path类抽象路径表示,自动适配不同操作系统路径分隔符,并提供exists、is_directory、create_directory等函数实现跨平台文件操作,结合try-catch或error_code处理异常,避免程序崩溃,同时可借助chrono库将file_time_type转换为可读的日期时间格式。

C++17文件系统库主要用于跨平台的文件和目录操作,它提供了一套标准接口,解决了不同操作系统路径表示方式的差异,让代码更具可移植性。
跨平台路径操作的核心在于使用
std::filesystem::path
解决方案
包含头文件: 首先,需要在代码中包含
<filesystem>
立即学习“C++免费学习笔记(深入)”;
#include <filesystem> #include <iostream> namespace fs = std::filesystem; // 建议使用命名空间别名
创建路径对象: 使用
std::filesystem::path
std::string
fs::path p1 = "/home/user/documents"; // 绝对路径 fs::path p2 = "data/input.txt"; // 相对路径 fs::path p3 = p1 / p2; // 路径拼接
路径操作:
std::filesystem::path
p.filename()
p.parent_path()
p.extension()
p.is_absolute()
p.lexically_normal()
.
..
fs::path p = "/home/user/documents/report.pdf"; std::cout << "Filename: " << p.filename() << std::endl; // 输出: report.pdf std::cout << "Parent path: " << p.parent_path() << std::endl; // 输出: /home/user/documents std::cout << "Extension: " << p.extension() << std::endl; // 输出: .pdf
文件系统操作: C++17文件系统库提供了许多函数用于执行实际的文件系统操作。
fs::exists(p)
fs::is_regular_file(p)
fs::is_directory(p)
fs::create_directory(p)
fs::remove(p)
fs::copy(from, to)
fs::file_size(p)
fs::last_write_time(p)
fs::path dir = "mydir";
if (!fs::exists(dir)) {
if (fs::create_directory(dir)) {
std::cout << "Directory created successfully." << std::endl;
} else {
std::cerr << "Failed to create directory." << std::endl;
}
}
fs::path file = dir / "myfile.txt";
std::ofstream outfile(file);
if (outfile.is_open()) {
outfile << "Hello, world!" << std::endl;
outfile.close();
}
if (fs::exists(file) && fs::is_regular_file(file)) {
std::cout << "File size: " << fs::file_size(file) << std::endl;
}遍历目录: 使用
fs::directory_iterator
fs::recursive_directory_iterator
fs::path dir_to_scan = "."; // 当前目录
for (const auto& entry : fs::directory_iterator(dir_to_scan)) {
std::cout << entry.path() << std::endl;
}
// 递归遍历
for (const auto& entry : fs::recursive_directory_iterator(dir_to_scan)) {
std::cout << entry.path() << std::endl;
}C++17文件系统库如何处理不同操作系统的路径差异?
C++17文件系统库通过
std::filesystem::path
\
/
/
文件系统库的异常处理机制是什么?如何避免程序崩溃?
文件系统操作可能会抛出异常,例如文件不存在、权限不足等。 C++17文件系统库提供了两种处理异常的方式:
抛出异常: 默认情况下,文件系统函数在遇到错误时会抛出
std::filesystem::filesystem_error
try-catch
try {
fs::remove("nonexistent_file.txt");
} catch (const fs::filesystem_error& e) {
std::cerr << "Error removing file: " << e.what() << std::endl;
}错误码: 可以传递一个
std::error_code
std::error_code
std::error_code
std::error_code ec;
fs::remove("nonexistent_file.txt", ec);
if (ec) {
std::cerr << "Error removing file: " << ec.message() << std::endl;
}为了避免程序崩溃,建议始终使用
try-catch
std::error_code
如何使用文件系统库获取文件的最后修改时间,并将其转换为可读的日期时间格式?
使用
fs::last_write_time(p)
std::filesystem::file_time_type
<chrono>
#include <chrono>
#include <iomanip> // 用于格式化输出
fs::path file_path = "myfile.txt";
if (fs::exists(file_path)) {
auto last_write_time = fs::last_write_time(file_path);
// 将 file_time_type 转换为 system_clock::time_point
auto system_time = std::chrono::clock_cast<std::chrono::system_clock>(last_write_time);
// 将 time_point 转换为 time_t
std::time_t tt = std::chrono::system_clock::to_time_t(system_time);
// 使用 localtime 将 time_t 转换为 tm 结构体
std::tm* ttm = std::localtime(&tt);
if (ttm != nullptr) {
// 格式化输出
std::cout << "Last write time: " << std::put_time(ttm, "%Y-%m-%d %H:%M:%S") << std::endl;
} else {
std::cerr << "Error converting time." << std::endl;
}
} else {
std::cerr << "File not found." << std::endl;
}这段代码首先获取文件的最后修改时间,然后将其转换为
std::chrono::system_clock::time_point
std::time_t
std::localtime
std::tm
std::put_time
localtime
以上就是C++17文件系统库怎么用 跨平台路径操作新特性的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号