c++kquote>C++17引入std::filesystem,提供跨平台文件目录操作统一接口。需编译器支持C++17,包含头文件,使用std::filesystem命名空间。核心类path处理路径,支持自动分隔符适配;status与is_*函数判断路径属性;create_directory、remove等函数管理目录;directory_iterator遍历目录;rename、copy实现文件移动复制;需捕获filesystem_error异常保证健壮性。

从C++17开始,标准库引入了 std::filesystem 模块,极大简化了文件和目录的跨平台操作。在此之前,开发者通常依赖操作系统原生API(如Windows的Win32 API或Linux的POSIX接口)或第三方库(如Boost.Filesystem),代码可移植性差。而 std::filesystem 提供了一套统一、简洁、类型安全的接口,支持Windows、Linux、macOS等主流平台。
启用 std::filesystem
要使用 std::filesystem,需满足以下条件:
编译器支持 C++17 或更高版本 包含头文件:#include 使用命名空间:using namespace std::filesystem;(可选)
在GCC、Clang和MSVC中,通常只需添加编译选项 -std=c++17 即可启用。
常用路径操作:path 类
std::filesystem::path 是所有文件系统操作的核心类,用于表示路径,支持跨平台路径分隔符自动处理(Windows用反斜杠,Unix系用正斜杠/)。
立即学习“C++免费学习笔记(深入)”;
示例:
path p = "/home/user/documents"; // Linuxpath p2 = "C:UsersJohnDesktop"; // Windowspath p3 = "folder/file.txt";cout << p3.filename() << endl; // 输出: file.txtcout << p3.stem() << endl; // 输出: filecout << p3.extension() << endl; // 输出: .txtpath combined = p / "report.pdf"; // 路径拼接,自动适配分隔符
文件与目录状态判断
通过 status 和 is_* 系列函数可判断路径类型和属性:
is_regular_file(p):是否为普通文件 is_directory(p):是否为目录 is_symlink(p):是否为符号链接 exists(p):路径是否存在 file_size(p):获取文件大小(字节)
示例:
path p = "test.txt";if (exists(p)) { if (is_regular_file(p)) cout << "文件大小: " << file_size(p) << " 字节" << endl; else if (is_directory(p)) cout << "这是一个目录" << endl;} else { cout << "路径不存在" << endl;}
创建、删除与遍历目录
std::filesystem 提供了简洁的目录管理接口:
create_directory(p):创建单个目录 create_directories(p):递归创建多级目录 remove(p):删除文件或空目录 remove_all(p):递归删除目录及其内容
遍历目录使用 directory_iterator 或 recursive_directory_iterator:
// 遍历当前目录for (const auto& entry : directory_iterator(".")) { cout << entry.path() << endl;}// 递归遍历子目录for (const auto& entry : recursive_directory_iterator("data")) { if (entry.is_regular_file() && entry.path().extension() == ".txt") cout << "找到文本文件: " << entry.path() << endl;}
文件重命名与复制
移动、重命名和复制文件也变得非常简单:
rename(old, new):重命名或移动文件/目录 copy(source, target):复制文件 copy_options::overwrite_existing:允许覆盖
示例:
// 复制并允许覆盖copy("a.txt", "backup/a.txt", copy_options::overwrite_existing);// 重命名文件rename("old_name.txt", "new_name.txt");// 移动目录rename("temp/", "archive/temp/");
基本上就这些。std::filesystem 让C++具备了现代语言应有的文件系统操作能力,无需再写平台相关的代码。只要编译环境支持C++17,就可以放心使用这套接口进行跨平台开发。注意在实际项目中捕获可能抛出的 filesystem_error 异常,以增强健壮性。不复杂但容易忽略。
以上就是C++如何跨平台操作文件和目录_C++17标准库std::filesystem的使用教程的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1487200.html
微信扫一扫
支付宝扫一扫