当前位置: 首页 > article >正文

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;
}
 


http://www.kler.cn/news/319313.html

相关文章:

  • 一篇Spring IOC笔记
  • Spring底层原理大致脉络
  • QT窗口无法激活弹出问题排查记录
  • 给子组件传递dom元素引用实例方案
  • 浮点型的详细介绍以及sizeof
  • 灵当CRM index.php SQL注入漏洞复现
  • C语言之初阶指针
  • 【机器学习】自监督学习:解锁数据的无限潜能
  • 大数据-146 Apache Kudu 安装运行 Dockerfile 模拟集群 启动测试
  • ubuntu 安装minikube,并拉取k8s镜像
  • 2024年9月SCI-苔藓生长优化算法Moss Growth Optimization-附Matlab免费代码
  • 线性代数书中求解线性方程组的三种方法的实例
  • C标准库<string.h>-mem开头的函数
  • Linux安装Redis
  • 使用vite+react+ts+Ant Design开发后台管理项目(三)
  • 5G技术对IT行业的影响及未来发展
  • SpringBoot整合ELK实现日志监控(保姆级教程)
  • fo-dicom,第一个基于.NET Standard 2.0 开发的DICOM开源库
  • 【ANTLR】常见的几种编程语言表达模式
  • 古代经典名方目录数据库-支持经典名方检索!
  • IMS注册流程中的基本路由寻址过程
  • 西部移动硬盘怎么恢复数据?4种详细且实用的方法
  • 腾讯邮箱上传附件卡、慢、无法上传问题处理
  • 详解机器学习经典模型(原理及应用)——逻辑回归
  • neo4j小白入门
  • 记录踩坑 uniapp 引入百度地图(微信小程序,H5,APP)
  • 研一上课计划2024/9/23有感
  • 高效编程的利器 Jupyter Notebook
  • Java Map类
  • 《AI办公类文档工具系列之三——ChatPDF》