c++中如何遍历文件夹下的所有文件_C++递归遍历目录文件实现方法

32次阅读
c++kquote>使用C++17 filesystem可跨平台递归遍历目录,推荐此方法;Windows可用FindFirstFile</code> API,Linux/Unix可用dirent.h,注意路径分隔符与编码处理。

c++中如何遍历文件夹下的所有文件_C++递归遍历目录文件实现方法</p>

在C++中遍历文件夹下的所有文件,尤其是包含子目录的递归遍历,可以通过不同平台的API或跨平台库来实现。下面分别介绍使用Windows API、POSIX(Linux/mac</a>OS)以及现代C++17标准中的

<filesystem></code></code></pre>

</div></div>方法。</p>

使用C++17 filesystem(推荐)</h3> C++17引入了

<filesystem></code></code></pre>

</div></div>头文件,提供了跨平台的文件系统操作支持,是目前最简洁、安全的方式。</font>

示例代码:</strong></p> #include <ios</a>tream>
#include <filesystem></code></code>
namespace fs = std::filesystem</code>;

void traverse(const fs::path& path) {
    for (const auto& entry : fs::recursive_directory_iterator(path)) {
        if (entry.is_regular_file()) {
            std::cout << “File: ” << entry.path().string() << ‘ ‘;
        } else if (entry.is_directory()) {
            std::cout << “Dir: ” << entry.path().string() << ‘ ‘;
        }
    }
}

</p>

int mai</a>n() {
    traverse(“C:/example”); // 替换为你的路径
    return 0;
} </p></font>

编译时需启用C++17支持,例如g++:
g++ -std=c++17 main.cpp -o main</font></p>

Windows平台:使用Win32 API</h3> 在Windows下可使用

FindFirstFile</code></pre>

</div></div>和

FindNextFile</code></pre>

</div></div>进行递归遍历。</font>

示例代码:</strong></p> #include <iostream>
#include <
windows</a>.h>
#include <string>

void traverse_win</a>32(const std::string& path) {
    std::string searchPath = path + “*”;
    WIN32_FIND_DATAA data;
    HANDLE hFind = FindFirstFile</code>A(searchPath.c_str(), &data);

</p>

    if (hFind == INVALID_HANDLE_VALUE) return;

</p>

立即学习</span>“//pan.quark.cn/s/6e7abc4abb9f" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">C++免费学习笔记(深入)</a>”;</p>

    do {
        if (std::string(data.cFileName) == “.” || std::string(data.cFileName) == “..”)
            continue;

</p>

        std::string fullPath = path + “” + data.cFileName;

</p>

//phps.yycxw.com/ai/galileo-ai">c++中如何遍历文件夹下的所有文件_C++递归遍历目录文件实现方法</a>

//phps.yycxw.com/ai/galileo-ai">Galileo AI</a>

AI生成可编辑的UI界面</p>

c++中如何遍历文件夹下的所有文件_C++递归遍历目录文件实现方法28</span> </div> </div> //phps.yycxw.com/ai/galileo-ai"> 查看详情</span> c++中如何遍历文件夹下的所有文件_C++递归遍历目录文件实现方法</a> </div>

        if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            std::cout << “Dir: ” << fullPath << ‘ ‘;
            traverse_win32(fullPath); // 递归进入子目录
        } else {
            std::cout << “File: ” << fullPath << ‘ ‘;
        }
    } while (FindNextFile</code>A(hFind, &data));

</p>

    FindClose(hFind);
}

</p>

int main() {
    traverse_win32(“C:example”);
    return 0;
} </p></font>

Linux/Unix:使用dirent.h</h3> 在POSIX系统中,可以使用

<dirent.h></code></pre>

</div></div>和

<sys/stat.h></code></pre>

</div></div>进行递归遍历。</font>

示例代码:</strong></p> #include <iostream>
#include <dirent.h></code>
#include <sys/stat.h></code>
#include <string>
#include <vector>

bool is_directory(const std::string& path) {
    struct stat st;
    return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
}

</p>

void traverse_linux</a>(const std::string& path) {
    DIR* dir = opendir(path.c_str());
    if (!dir) return;

</p>

    struct dirent* entry;
    while ((entry = readdir(dir)) != nullptr) {
        std::string name = entry->d_name;
        if (name == “.” || name == “..”) continue;

</p>

        std::string fullPath = path + “/” + name;

</p>

        if (is_directory(fullPath)) {
            std::cout << “Dir: ” << fullPath << ‘ ‘;
            traverse_linux(fullPath);
        } else {
            std::cout << “File: ” << fullPath << ‘ ‘;
        }
    }

</p>

    closedir(dir);
}

</p>

int main() {
    traverse_linux(“/home/user/example”);
    return 0;
} </p></font>

注意事项与建议</h3> – 推荐优先使用C++17的

std::filesystem</code></pre>

</div></div>,代码简洁且跨平台。
– 注意路径分隔符:Windows用反斜杠

</pre>

</div></div>,Linux用

/</code></pre>

</div></div>,可用条件编译或统一使用

/</pre>

</div></div>(多数系统支持)。
– 递归深度过大可能导致栈</a>溢出,可改用栈结构模拟递归。
– 处理中文路径时确保
编码</a>一致,Windows建议使用宽字符版本API(如FindFirstFile</code>W)。</font>

基本上就这些,选择合适的方法取决于你的目标平台和C++标准支持情况。</p></blo>

</p>

相关标签:</span>

linux</a> windows</a> 编码</a> mac</a> 栈</a> ai</a> unix</a> c++</a> ios</a> macos</a> win</a> cos</a> String</a> if</a> for</a> while</a> include</a> Filesystem</a> const</a> auto</a> continue</a> 递归</a> bool</a> int</a> void</a> 栈</a> Struct</a> Namespace</a> windows</a> macos</a> linux</a> unix</a> </div> </div>

大家都在看:</h2>

//phps.yycxw.com/faq/1461177.html" title="Linux系统如何配置C++编译环境 GCC和Clang安装教程">Linux系统如何配置C++编译环境 GCC和Clang安装教程</a> //phps.yycxw.com/faq/1457630.html" title="怎样用C++实现文件权限管理 Windows与Linux系统差异处理">怎样用C++实现文件权限管理 Windows与Linux系统差异处理</a> //phps.yycxw.com/faq/1442432.html" title="C++嵌入式Linux驱动开发环境怎么搭建 Yocto项目定制化配置">C++嵌入式Linux驱动开发环境怎么搭建 Yocto项目定制化配置</a> //phps.yycxw.com/faq/1429201.html" title="如何搭建C++的嵌入式Linux环境 使用Yocto构建定制系统">如何搭建C++的嵌入式Linux环境 使用Yocto构建定制系统</a> //phps.yycxw.com/faq/1379245.html" title="高频交易系统:如何突破Linux内核调度限制">高频交易系统:如何突破Linux内核调度限制</a> </div> </div> </div> linux</a> windows</a> 编码</a> mac</a> 栈</a> ai</a> unix</a> c++</a> ios</a> macos</a> win</a> cos</a> String</a> if</a> for</a> while</a> include</a> Filesystem</a> const</a> auto</a> continue</a> 递归</a> bool</a> int</a> void</a> 栈</a> Struct</a> Namespace</a> windows</a> macos</a> linux</a> unix</a>

Copyright ©  SEO

 Theme by Puock