c++常用工具类函数

递归读取文件名

void listFiles(const std::string& path,std::vector<std::string>&vec) {

    DIR* dir;

    struct dirent* entry;

    struct stat statbuf;

    if ((dir = opendir(path.c_str())) == nullptr) {

        std::cerr << "Error opening directory: " << path << std::endl;

        return;

    }

    while ((entry = readdir(dir)) != nullptr) {

        std::string entryName = entry->d_name;

        std::string fullPath = path + "/" + entryName;

        if (entryName != "." && entryName != "..") {

            if (stat(fullPath.c_str(), &statbuf) == -1) {

                std::cerr << "Error getting file stats for: " << fullPath << std::endl;

                continue;

            }

            if (S_ISDIR(statbuf.st_mode)) {

                // 递归调用以处理子目录

                listFiles(fullPath,vec);

            } else {

                // 打印文件名

               // std::cout << fullPath << std::endl;

                vec.push_back(fullPath);

            }

        }

    }

    closedir(dir);

}

递归创建目录

#include <iostream>
#include <string>
#include <vector>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

// 用于分割路径的函数
std::vector<std::string> split(const std::string &path, const std::string &delimiter) {
    std::vector<std::string> tokens;
    size_t start = 0, end = 0;
    while ((end = path.find(delimiter, start)) != std::string::npos) {
        tokens.push_back(path.substr(start, end - start));
        start = end + delimiter.length();
    }
    tokens.push_back(path.substr(start));
    return tokens;
}

// 递归创建目录的函数
bool createDirectories(const std::string &path) {
    // 分割路径
    std::vector<std::string> pathParts = split(path, "/");
    //get current directory
    char buff[1024];
    if (getcwd(buff, sizeof(buff)) != nullptr) {
        std::cout << "Current path: " << buff << std::endl;
    } else {
        perror("getcwd() error");
    }

    std::string currentPath=std::string(buff);
    // 逐级创建目录
    for (size_t i = 0; i < pathParts.size(); ++i) {
        currentPath += "/" + pathParts[i];
        // 跳过空字符串
        if (currentPath == "/") {
            continue;
        }
        // 跳过非目录部分
        if (i < pathParts.size() - 1) {
            struct stat st;
            if (stat(currentPath.c_str(), &st) != 0) {
                // 如果目录不存在,则创建它
                if (mkdir(currentPath.c_str(), 0755) == -1) {
                    // 如果创建失败,并且不是因为目录已存在
                    if (errno != EEXIST) {
                        std::cerr << "Error creating directory: " << currentPath << std::endl;
                        return false;
                    }
                }
            }
        }
    }
    return true;
}

int test() {
    std::string dirPath = "path/to/your/directory";
    if (createDirectories(dirPath)) {
        std::cout << "Directories created successfully" << std::endl;
    } else {
        std::cerr << "Failed to create directories" << std::endl;
    }
    return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值