Linux下获取文件夹下普通文本文件和子文件夹中的文件的路径和所有文件的字节数之和

#include <iostream>
#include <string>
#include <vector>
#include <dirent.h>
#include <string.h>
#include <stack>
#include <sys/stat.h>

// 获取文件字节数
uint64_t GetFileSize(const std::string& filepath)
{
    struct stat statbuf;  
    stat(filepath.c_str(), &statbuf);
    return statbuf.st_size;
}

// 打印文件夹中所有文件的路径(含子文件夹中的文件) —— 递归版本
std::vector<std::string> get_all_files_in_directory(const std::string& directory) 
{
    std::vector<std::string> file_paths;
    DIR* dir = opendir(directory.c_str());
    if (dir == nullptr) 
    {
        std::cerr << "无法打开目录: " << directory << std::endl;
        return file_paths;
    }

    struct dirent* dirEntry;
    while ((dirEntry = readdir(dir)) != nullptr)
     {
        if (dirEntry->d_type == DT_REG) 
        { 
            // 普通文件
            std::string file_path = directory + "/" + dirEntry->d_name;
            file_paths.push_back(file_path);
        } 
        else if (dirEntry->d_type == DT_DIR && strcmp(dirEntry->d_name, ".")!=0 && strcmp(dirEntry->d_name, "..")!=0 ) 
        { 
            // 子文件夹
            std::vector<std::string> sub_file_paths = get_all_files_in_directory(directory+"/"+dirEntry->d_name);
            file_paths.insert(file_paths.end(), sub_file_paths.begin(), sub_file_paths.end());
        }
    }

    closedir(dir);
    return file_paths;
}

// 打印文件夹中所有文件的路径(含子文件夹中的文件) —— 循环版本
std::vector<std::string> GetAllFilesInDirectory(const std::string& directory)
{
    // 初始文件目录所占用的空间
    uint64_t totalSize = 4096;  //视文件系统而定;  解决办法: +1MB作为容错处理!

    std::vector<std::string> file_paths;
    std::stack<std::string> dirs;
    DIR* dir = opendir(directory.c_str());

    if(dir == nullptr)
    {
        std::cerr << "无法打开目录: " << directory << std::endl;
        return file_paths;
    }

    dirs.push(directory);

    while(!dirs.empty())
    {
        std::string currentDir = dirs.top();
        dirs.pop();
        DIR* sub_dir = opendir(currentDir.c_str());
        if(sub_dir == nullptr)
        {
            std::cerr << "无法打开子目录: " << currentDir << std::endl;
            continue;
        }
        struct dirent* dirEntry;
        while((dirEntry = readdir(sub_dir)) != nullptr)
        {
            if(dirEntry->d_type == DT_REG)
            {
                // 普通文件
                std::string file_path = currentDir + "/" + dirEntry->d_name;
                file_paths.push_back(file_path);
                totalSize += GetFileSize(file_path);
            }
            else if(dirEntry->d_type == DT_DIR && strcmp(dirEntry->d_name, ".")!=0 && strcmp(dirEntry->d_name, "..")!=0 )
            {
                // 子文件夹
                std::string subDir = currentDir + "/" + dirEntry->d_name;
                dirs.push(subDir);
                totalSize += 4096;  //每一级子目录也占用4096个字节
            }
        }
        closedir(sub_dir);
    }
    closedir(dir);

    std::cout << "totalSize = " << totalSize <<std::endl;
    return file_paths;
}

int main() 
{
    std::string directory = "/home/wang/downloads/loglog";
    std::vector<std::string> dirFilePath;

    std::vector<std::string> file_paths1 = GetAllFilesInDirectory(directory);
    std::vector<std::string> file_paths2 = get_all_files_in_directory(directory);

    uint64_t size1 = 0;
    for (const auto& file_path : file_paths1) 
    {
        std::cout << file_path << std::endl;
        size1 += GetFileSize(file_path);
    }

    uint64_t size2 = 0;
    for (const auto& file_path : file_paths2) 
    {
        std::cout << file_path << std::endl;
        size2 += GetFileSize(file_path);
    }
    std::cout << "size1 = " << size1 << std::endl;
    std::cout << "size2 = " << size2 << std::endl;

    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "file_paths1.size = " << file_paths1.size() << std::endl;
    std::cout << "file_paths2.size = " << file_paths2.size() << std::endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值