C++17的<filesystem>模块通过统一跨平台API、提供路径安全操作和异常处理机制,简化了目录的创建、删除与遍历,避免了系统差异和字符串误操作,成为现代C++文件系统操作的首选方案。

C++中对目录进行创建、删除和遍历,在现代C++(特别是C++17及更高版本)中,主要通过标准库中的
<filesystem>
要实现C++的目录操作,我们推荐使用C++17引入的
std::filesystem
创建目录
使用
std::filesystem::create_directory
std::filesystem::create_directories
立即学习“C++免费学习笔记(深入)”;
#include <iostream>
#include <filesystem> // C++17
void create_directories_example() {
std::filesystem::path dir_path = "my_new_dir/sub_dir";
try {
if (std::filesystem::create_directories(dir_path)) {
std::cout << "Successfully created directory: " << dir_path << std::endl;
} else {
// 目录可能已经存在,或者其他原因导致未创建
if (std::filesystem::exists(dir_path)) {
std::cout << "Directory already exists: " << dir_path << std::endl;
} else {
std::cout << "Failed to create directory: " << dir_path << std::endl;
}
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error creating directory: " << e.what() << std::endl;
}
}删除目录
std::filesystem::remove
std::filesystem::remove_all
remove_all
#include <iostream>
#include <filesystem>
void remove_directories_example() {
std::filesystem::path dir_to_delete = "my_new_dir"; // 假设这个目录存在且可能非空
try {
if (std::filesystem::exists(dir_to_delete)) {
// remove_all 返回删除的条目数量
std::uintmax_t removed_count = std::filesystem::remove_all(dir_to_delete);
std::cout << "Successfully removed directory and its contents: "
<< dir_to_delete << ". Removed " << removed_count << " items." << std::endl;
} else {
std::cout << "Directory does not exist: " << dir_to_delete << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error removing directory: " << e.what() << std::endl;
}
}遍历目录
std::filesystem::directory_iterator
std::filesystem::recursive_directory_iterator
#include <iostream>
#include <filesystem>
#include <string>
void traverse_directory_example() {
std::filesystem::path target_dir = "."; // 遍历当前目录
std::cout << "Traversing directory: " << target_dir << std::endl;
// 非递归遍历
std::cout << "\n--- Non-recursive traversal ---" << std::endl;
try {
for (const auto& entry : std::filesystem::directory_iterator(target_dir)) {
std::cout << " " << entry.path().filename();
if (entry.is_directory()) {
std::cout << " (Directory)";
} else if (entry.is_regular_file()) {
std::cout << " (File, size: " << entry.file_size() << " bytes)";
} else if (entry.is_symlink()) {
std::cout << " (Symlink -> " << std::filesystem::read_symlink(entry.path()) << ")";
}
std::cout << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error during non-recursive traversal: " << e.what() << std::endl;
}
// 递归遍历
std::cout << "\n--- Recursive traversal ---" << std::endl;
try {
for (const auto& entry : std::filesystem::recursive_directory_iterator(target_dir)) {
std::string indent(entry.depth() * 2, ' '); // 根据深度增加缩进
std::cout << indent << entry.path().filename();
if (entry.is_directory()) {
std::cout << " (Directory)";
} else if (entry.is_regular_file()) {
std::cout << " (File)";
}
std::cout << std::endl;
}
} catch (const std::filesystem::filesystem_error& e) {
std::cerr << "Error during recursive traversal: " << e.what() << std::endl;
}
}
// 可以在main函数中调用这些示例
/*
int main() {
create_directories_example();
// create some files/subdirs in my_new_dir for remove_directories_example to test remove_all
// std::ofstream("my_new_dir/file1.txt").close();
// std::filesystem::create_directory("my_new_dir/another_sub");
// std::ofstream("my_new_dir/another_sub/file2.txt").close();
remove_directories_example();
traverse_directory_example();
return 0;
}
*/<filesystem>
在C++17之前,处理文件系统操作一直是个令人头疼的问题。标准库在这方面是空白的,这意味着开发者不得不依赖于操作系统特定的API,比如Windows上的
CreateDirectory
RemoveDirectory
FindFirstFile
mkdir
rmdir
opendir
readdir
std::filesystem
std::filesystem
std::filesystem::path
<filesystem>
目录操作看似简单,但在实际项目中却充满了各种微妙的陷阱,如果不小心,可能会导致程序崩溃、数据丢失甚至安全漏洞。
常见陷阱:
/
\
..
.
rmdir
std::filesystem::remove_all
std::filesystem::path
最佳实践:
std::filesystem
std::filesystem::filesystem_error
std::error_code
std::filesystem::path
std::filesystem::path
/
remove
remove_all
remove_all
directory_iterator
recursive_directory_iterator
copy
copy_options
entry.is_symlink()
std::filesystem::read_symlink
std::filesystem::symlink_status
std::filesystem
路径查询与信息获取:
std::filesystem::exists(p)
p
std::filesystem::is_directory(p)
p
std::filesystem::is_regular_file(p)
p
std::filesystem::is_symlink(p)
p
std::filesystem::file_size(p)
std::filesystem::last_write_time(p)
std::filesystem::status(p)
std::filesystem::symlink_status(p)
路径操作与转换:
std::filesystem::absolute(p)
p
std::filesystem::canonical(p)
p
..
.
std::filesystem::relative(p, base)
p
base
std::filesystem::current_path()
std::filesystem::current_path(p)
path::filename()
path::stem()
path::extension()
path::parent_path()
文件和目录的复制与移动:
std::filesystem::copy(from, to, options)
options
std::filesystem::copy_file(from, to, options)
std::filesystem::rename(old_path, new_path)
权限管理:
std::filesystem::permissions(p, prms, options)
prms
options
临时文件和目录:
std::filesystem::temp_directory_path()
path::unique()
create_directory
这些高级功能使得
std::filesystem
recursive_directory_iterator
file_size
last_write_time
以上就是C++目录操作实现 创建删除遍历目录的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号