首页 > 后端开发 > C++ > 正文

c++如何遍历目录下的所有文件_c++遍历文件系统目录的方法

穿越時空
发布: 2025-10-31 20:28:02
原创
525人浏览过
c++kquote>C++中遍历目录推荐使用C++17的std::filesystem,跨平台且简洁;也可根据系统选择POSIX或Windows API。

c++如何遍历目录下的所有文件_c++遍历文件系统目录的方法

在C++中遍历目录下的所有文件,可以使用不同方法,具体取决于你使用的平台和标准库支持程度。以下是几种常见且实用的方式。

使用 C++17 的 std::filesystem(推荐)

从 C++17 开始,标准库提供了 std::filesystem 模块,可以跨平台地遍历目录,无需依赖第三方库。

你需要包含头文件 filesystem 并使用 std::filesystem::directory_iteratorrecursive_directory_iterator 来遍历。

示例代码:

立即学习C++免费学习笔记(深入)”;

笔目鱼英文论文写作器
笔目鱼英文论文写作器

写高质量英文论文,就用笔目鱼

笔目鱼英文论文写作器87
查看详情 笔目鱼英文论文写作器
#include <iostream>
#include <filesystem>
<p>namespace fs = std::filesystem;</p><p>void traverse_directory(const std::string& path) {
for (const auto& entry : fs::directory_iterator(path)) {
std::cout << entry.path() << "
";
}
}</p><p>// 递归遍历子目录
void traverse_recursive(const std::string& path) {
for (const auto& entry : fs::recursive_directory_iterator(path)) {
std::cout << entry.path() << "
";
}
}</p><p>int main() {
std::string dir = "."; // 当前目录
traverse_directory(dir);
return 0;
}
登录后复制

编译时注意: 需要启用 C++17 并链接 stdc++fs(GCC),例如:

g++ -std=c++17 your_file.cpp -lstdc++fs
登录后复制

使用 POSIX opendir / readdir(Linux/Unix)

在类 Unix 系统中,可以使用 <dirent.h> 提供的函数进行目录遍历。

示例代码:

立即学习C++免费学习笔记(深入)”;

#include <iostream>
#include <dirent.h>
<p>void traverse_posix(const std::string& path) {
DIR* dir = opendir(path.c_str());
if (!dir) {
std::cerr << "无法打开目录: " << path << "
";
return;
}</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
    std::string name = entry->d_name;
    if (name != "." && name != "..") {
        std::cout << path + "/" + name << "
";
    }
}
closedir(dir);
登录后复制

}

该方法不支持递归遍历子目录中的子目录自动展开,需手动判断类型并递归调用。

使用 Windows API(Windows 平台)

在 Windows 上,可以使用 FindFirstFileFindNextFile 函数遍历目录。

示例代码:

立即学习C++免费学习笔记(深入)”;

#include <iostream>
#include <windows.h>
<p>void traverse_windows(const std::string& path) {
std::string searchPath = path + "*";
WIN32_FIND_DATA data;
HANDLE hFind = FindFirstFile(searchPath.c_str(), &data);</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">if (hFind == INVALID_HANDLE_VALUE) {
    std::cerr << "无法打开目录
";
    return;
}

do {
    std::string name = data.cFileName;
    if (name != "." && name != "..") {
        std::cout << path + "\" + name << "
";
    }
} while (FindNextFile(hFind, &data));

FindClose(hFind);
登录后复制

}

适用于原生 Windows 编程,但不具备跨平台性。

小结与建议

如果你的编译器支持 C++17,强烈推荐使用 std::filesystem,它简洁、安全、跨平台。

若项目受限于旧标准或特定平台,可选择对应平台的 API 实现。

基本上就这些方法,根据环境选择即可。

以上就是c++++如何遍历目录下的所有文件_c++遍历文件系统目录的方法的详细内容,更多请关注php中文网其它相关文章!

c++速学教程(入门到精通)
c++速学教程(入门到精通)

c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号