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

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【sgCreateCallAPIFunctionParam】自定义小工具:敏捷开发→调用接口方法参数生成工具
  • Score-based Generative Models
  • 前端接口报错302 [已解决]
  • 多米诺骨牌游戏
  • Python提供内置正则表达式库
  • 大数据-147 Apache Kudu 常用 Java API 增删改查
  • 回归阅读第一本:《瓦尔纳宝典》
  • 使用电脑当服务器,来组建局域网是否安全
  • mini-lsm通关笔记Week2Overview
  • IT行业的未来:技术变革与创新的持续推动
  • 肺结节检测-目标检测数据集(包括VOC格式、YOLO格式)
  • 复试经验分享《一、问答题自测》(408、相关前沿技术)
  • 【计算机网络篇】电路交换,报文交换,分组交换
  • echarts 导出pdf空白原因
  • 分析二极管的交流响应(1)——直流分析,Q点的计算
  • AWS实战 - 利用IAM对S3做访问控制
  • ERLANG 网工修炼笔记 ---- UDP
  • ES6简单总结(搭配简单的讲解和小案例)
  • golang 发送GET和POST示例
  • IOS评论框不贴底(ios12新bug)
  • Javascript Math对象和Date对象常用方法详解
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • vue总结
  • Yii源码解读-服务定位器(Service Locator)
  • 编写符合Python风格的对象
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 开源地图数据可视化库——mapnik
  • 蓝海存储开关机注意事项总结
  • 两列自适应布局方案整理
  • 深入 Nginx 之配置篇
  • 算法之不定期更新(一)(2018-04-12)
  • 用jquery写贪吃蛇
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 智能合约开发环境搭建及Hello World合约
  • Spark2.4.0源码分析之WorldCount 默认shuffling并行度为200(九) ...
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • #微信小程序:微信小程序常见的配置传旨
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (2)MFC+openGL单文档框架glFrame
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (javascript)再说document.body.scrollTop的使用问题
  • (二刷)代码随想录第15天|层序遍历 226.翻转二叉树 101.对称二叉树2
  • (四)stm32之通信协议
  • (算法)求1到1亿间的质数或素数
  • (一)SvelteKit教程:hello world
  • (转)linux下的时间函数使用
  • (转载)Linux网络编程入门
  • ***测试-HTTP方法
  • *_zh_CN.properties 国际化资源文件 struts 防乱码等
  • .bat批处理(一):@echo off
  • .NET CF命令行调试器MDbg入门(一)
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证
  • .net实现客户区延伸至至非客户区