您可以使用递归的方式来实现统计文件夹下所有文件包括子目录文件的数量。以下是一个示例代码:

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int countFiles(const fs::path& dir) {
    int count = 0;
    for (const auto& entry : fs::directory_iterator(dir)) {
        if (entry.is_directory()) {
            count += countFiles(entry.path()); // 递归统计子目录文件数量
        } else {
            count++;
        }
    }
    return count;
}

int main() {
    fs::path dir("路径"); // 替换为文件夹的路径
    int fileCount = countFiles(dir);
    std::cout << "文件数量: " << fileCount << std::endl;
    
    return 0;
}

请将代码中的"路径"替换为您要统计的文件夹路径。代码使用了C++17中的std::filesystem库来进行文件和目录操作。countFiles函数使用递归的方式统计文件夹下所有文件包括子目录文件的数量。在main函数中调用countFiles函数,并输出结果。

C++ 统计文件夹下所有文件数量 (包括子目录)

原文地址: https://www.cveoy.top/t/topic/pY9k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录