C++文件系统操作6 - 跨平台实现查找指定文件夹下的特定文件

news2024/9/21 20:40:40

1. 关键词

C++ 文件系统操作 查找指定文件夹下的特定文件 跨平台

2. fileutil.h


#pragma once

#include <string>
#include <cstdio>
#include <cstdint>
#include "filetype.h"
#include "filepath.h"

namespace cutl
{

    /**
     * @brief The file guard class to manage the FILE pointer automatically.
     * file_guard object can close the FILE pointer automatically when his scope is exit.
     */
    class file_guard
    {
    public:
        /**
         * @brief Construct a new file guard object
         *
         * @param file the pointer of the FILE object
         */
        explicit file_guard(FILE *file);

        /**
         * @brief Destroy the file guard object
         *
         */
        ~file_guard();

        /**
         * @brief Get the FILE pointer.
         *
         * @return FILE*
         */
        FILE *getfd() const;

    private:
        FILE *file_;
    };

    /**
     * @brief Find all files in a directory by name.
     *
     * @param dirpath the filepath of the directory to be searched
     * @param name the name of the file to be found, substring match is supported.
     * @param recursive whether to search the whole directory recursively, default is false.
     * If true, means search the whole directory recursively, like the 'find' command.
     * @return filevec the vector of file_entity, filepaths in the directory.
     */
    filevec find_files(const filepath &dirpath, const std::string &name, bool recursive = false);

    /**
     * @brief Find all files in a directory by extension.
     *
     * @param dirpath the filepath of the directory to be searched
     * @param extension the extension of the file to be found, with dot, like ".txt".
     * @param recursive whether to search the whole directory recursively, default is false.
     * If true, means search the whole directory recursively, like the 'find' command.
     * @return filevec the vector of file_entity, filepaths in the directory.
     */
    filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive = false);

} // namespace cutl

3. fileutil.cpp

#include <cstdio>
#include <map>
#include <iostream>
#include <cstring>
#include <sys/stat.h>
#include "fileutil.h"
#include "inner/logger.h"
#include "inner/filesystem.h"
#include "strutil.h"

namespace cutl
{
    file_guard::file_guard(FILE *file)
        : file_(file)
    {
    }

    file_guard::~file_guard()
    {
        if (file_)
        {
            // CUTL_DEBUG("close file");
            int ret = fclose(file_);
            if (ret != 0)
            {
                CUTL_ERROR("fail to close file, ret" + std::to_string(ret));
            }
            file_ = nullptr;
        }
        // ROBOLOG_DCHECK(file_ == nullptr);
    }

    FILE *file_guard::getfd() const
    {
        return file_;
    }

    filevec find_files(const filepath &dirpath, const std::string &name, bool recursive)
    {
        filevec filelist = list_files(dirpath, filetype::all, recursive);
        filevec result;
        for (auto &file : filelist)
        {
            if (file.type == filetype::directory)
            {
                continue;
            }
            auto filename = cutl::path(file.filepath).basename();
            if (filename.find(name) != std::string::npos)
            {
                result.emplace_back(file);
            }
        }
        return result;
    }

    filevec find_files_by_extension(const filepath &dirpath, const std::string &extension, bool recursive)
    {
        filevec filelist = list_files(dirpath, filetype::all, recursive);
        filevec result;
        for (auto &file : filelist)
        {
            if (file.type == filetype::directory)
            {
                continue;
            }
            auto extname = cutl::path(file.filepath).extension();
            if (cutl::to_lower(extname) == cutl::to_lower(extension))
            {
                result.emplace_back(file);
            }
        }
        return result;
    }

4. list_files

list_files函数的定义参见: http://sunlogging.com/2024/07/25/cpp_common_util/fileutil_list_files/

5. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。


【SunLogging】
扫码二维码,关注微信公众号,精彩内容

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1951080.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

前端练习<HtmlCSS>——照片墙(附完整代码及实现效果)

这个小练习也来源于b站up小K师兄&#xff0c;大家可以通过下面的链接学习哦~up讲的非常详细。 纯CSS写一个简单酷炫的照片墙效果&#xff5e; 先看一下这个照片墙的效果&#xff1a; 1.鼠标没有放到图片上时&#xff0c;照片同比例&#xff0c;每张照片都有倒影的效果。 2.然…

linux怎么创建python

第一步&#xff0c;创建一个test文件夹。 第二步&#xff0c;打开终端进入该文件。 第三步&#xff0c;vim test.py。 第四步&#xff0c;编写代码。 第五步&#xff0c;编辑好之后&#xff0c;按Esc键切换到命令模式&#xff0c;然后输入:wq&#xff0c;再按回车键即可自动保存…

聊一聊知识图谱结合RAG

因为最近在做一些关于提高公司内部使用的聊天机器人的回答准确率&#xff0c;并且最近微软官方也是开源了一下graphrag的源码&#xff0c;所以想聊一聊这个知识图谱结合rag。 rag在利用私有数据增强大模型回答的领域是一种比较典型的技术&#xff0c;也就是我们提出问题的时候&…

MATLAB基础:数组及其数学运算

今天我们继续学习MATLAB中的数组 我们在学习MATLAB时了解到&#xff0c;MATLAB作者秉持着“万物皆可矩阵”的思想企图将数学甚至世间万物使用矩阵表示出来&#xff0c;而矩阵的处理&#xff0c;自然成了这门语言的重中之重。 数组基础 在MATLAB中&#xff0c;数组是一个基本…

LCD 横屏切换为竖屏-I.MX6U嵌入式Linux C应用编程学习笔记基于正点原子阿尔法开发板

LCD 横屏切换为竖屏 横屏显示如何切换为竖屏显示 LCD 屏默认横屏显示 开发板配套的 LCD 屏默认都是横屏显示&#xff0c;如 4.3 寸、7 寸和 10.1 寸的不同分辨率的 RGB LCD 屏 固定坐标体系 &#xff08;以 800*480 分辨率为例&#xff09;横屏模式下的固定坐标&#xff1a;…

【JavaScript】深入理解 `let`、`var` 和 `const`

文章目录 一、var 的声明与特点二、let 的声明与特点三、const 的声明与特点四、let、var 和 const 的对比五、实战示例六、最佳实践 在 JavaScript 中&#xff0c;变量声明是编程的基础&#xff0c;而 let、var 和 const 是三种常用的变量声明方式。本文将详细介绍这三种变量声…

Blackbox AI-跨时代AI产物,你的私人编程助手

1. 引言 随着人工智能技术的飞速发展&#xff0c;我们的生活方式正在经历前所未有的变革。从智能家居到自动驾驶&#xff0c;AI已经渗透到我们生活的方方面面。而在这场科技革命中&#xff0c;Blackbox 网站凭借其先进的技术和全面的功能&#xff0c;成为了众多AI产品中的佼佼者…

基于单片机控制的锂电池组电路的设计

摘 要: 提 出 一 种 基 于 单 片 机 控 制 的 锂 电 池 组 电 路 设计 方 案 . 采 用 8 位 CMOS 闪 存 单 片 机 PIC16F886 作 为主控芯 片 , 电 路 设计 中 含 有 S-8254 芯 片 的 一 次 保 护 电 路 、 S-8244 芯 片 的 二 次 保 护 电 路 和 MCU 的 辅 助 保 护 功…

photoshop学习笔记——选区3 快速选择工具

快速选择工具 W shift W 在3种快速选择工具之间切换 对象选择工具 photoshop CC中没有这个工具&#xff0c;利用AI&#xff0c;将款选中的对象快速的提取选区&#xff0c;测试了一下&#xff0c;选区制作的非常nice快速选择工具 跟磁性套索类似&#xff0c;自动识别颜色相似…

qt初入门9:qt记录日志的方式,日志库了解练习(qInstallMessageHandler,qslog, log4qt)

项目中用到qt&#xff0c;考虑有需要用到去记录日志&#xff0c;结合网络&#xff0c;整理一下&#xff0c;做记录。 简单了解后&#xff0c;qt实现日志模块思考&#xff1a; 1&#xff1a;借助qt自带的qInstallMessageHandler重定向到需要的目的地。 2&#xff1a;自己封装一…

JAVA SE 类和对象

类和对象 类定义和使用类的定义格式 类的实例化什么是实例化 this 引用this引用的特性 对象的构造及初始化如何初始化对象构造方法概念特性 在这里插入图片描述 **注意**&#xff1a; 封装封装的概念封装扩展之包导入包中的类自定义包包的访问权限控制举例 static成员static修饰…

MOZHE SQL手工注入漏洞测试(MySQL数据库)

主界面URL没有参数&#xff0c;无法判断是否有注入点 点击公告 【浏览器不便于查看返回包可以用burp】 测试URL 参数后加上单引号&#xff0c;报错&#xff0c;说明存在注入点 http://124.70.64.48:40021/new_list.php?id1 获取表列数 order by 4 返回200 order by 5 …

鸿蒙应用框架开发【N-Body模拟程序】

N-Body模拟程序 介绍 在本示例中&#xff0c;使用ArkTS编程语言开发了业界编程语言基准测试项目[Benchmarks Game]中的[N体问题模拟程序]&#xff0c;实现类木星体轨道计算。 本示例用到了ohos.taskpool和ohos.worker 接口。示例中的程序可以用于AOT(Ahead Of Time)等性能测…

计科录取75人!常州大学计算机考研考情分析!

常州大学&#xff08;Changzhou University&#xff09;&#xff0c;简称“常大”&#xff0c;位于江苏省常州市&#xff0c;是江苏省人民政府与中国石油天然气集团有限公司、中国石油化工集团有限公司及中国海洋石油集团有限公司共建的省属全日制本科院校&#xff0c;为全国深…

⼤模型在⽹络安全⽅⾯的应⽤汇总

引⾔ ⼤语⾔模型&#xff08;Large Language Models, LLMs&#xff09;的横空出世&#xff0c;为传统计算机科学的各个细分领域带来了颠覆性的变⾰。这种变⾰的浪潮同样席卷了⽹络安全领域&#xff0c;引发了⼀系列深刻的变化和影响。GPT-4、Gemini、Llama 2 等⼤模型以其卓越的…

7月22日学习笔记 文件共享服务nfs,SAMBA文件共享与DNS域名服务

任务背景 由于业务驱动&#xff0c;为了提⾼⽤户的访问效率&#xff0c;现需要将原有web服务器上的静态资源 ⽂件分离出来&#xff0c;单独保存到⼀台⽂件服务器上。 任务要求 1. ⼀台应⽤服务器web-server部署apache&#xff0c;静态⽹⻚资源存放在另外⼀台NFS服 务器上 …

深入理解计算机系统 CSAPP 家庭作业11.7

静态内容是指在不同请求中访问到的数据都相同的静态文件。例如&#xff1a;图片、视频、网站中的文件&#xff08;html、css、js&#xff09;、软件安装包、apk文件、压缩包文件等。 /** get_filetype - derive file type from file name*/ void get_filetype(char *filename,…

12_TypeScript 模块 以及 模块化封装DB 库

TypeScript 模块 1、模块中暴露方法12、模块中暴露方法23、模块中暴露方法34、封装[上一节的db 库](https://blog.csdn.net/qq_46143850/article/details/140664100)5、TypeScript 命名空间 模块的概念&#xff08;官方&#xff09;&#xff1a; 关于术语的一点说明&#xff1a…

Linux网络-wget命令

作者介绍&#xff1a;简历上没有一个精通的运维工程师。希望大家多多关注我&#xff0c;我尽量把自己会的都分享给大家&#xff0c;下面的思维导图也是预计更新的内容和当前进度(不定时更新)。 Linux服务器作为一个常用的网络服务器&#xff0c;主要的作用就是向客户端提供网络…

【工具推荐】强大的JS渗透测试工具:URLFinder

一、下载地址 https://github.com/pingc0y/URLFinder 二、工具原理 从表现中JS中提取URL或者敏感数据&#xff0c;一款用于快速提取检测页面中JS与URL的工具。功能类似于JSFinder&#xff0c;但JSFinder好久没更新了。 三、工具介绍 1、下载解压出来包含下面两个文件 2、直…