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

c++学习:容器list实战(获取目录返回容器list)

新建一个dir.h,声明dir类

#ifndef DIR_H
#define DIR_H#include <sys/types.h>#include <dirent.h>
#include <stdio.h>
#include <string.h>#include <iostream>
#include <list>class Dir
{
public:Dir();static std::list<std::string> entryList(const char *dirPath, const char *filter);
};#endif // DIR_H

新建一个dir.cpp,定义dir类

#include "dir.h"Dir::Dir()
{}std::list<std::string> Dir::entryList(const char *dirPath, const char *filter)
{std::list<std::string> list;DIR * fp = opendir(dirPath);if(fp == NULL){perror("opendir error");return list;}while(1){struct dirent * info = readdir(fp);if(info == NULL){break;}if(info->d_type == DT_REG && info->d_name[0] != '.' &&strstr(info->d_name,filter)!=NULL){//获取文件名char text[1024] = {0};if(dirPath[strlen(dirPath)-1] == '/'){sprintf(text,"%s%s",dirPath,info->d_name);}else{sprintf(text,"%s/%s",dirPath,info->d_name);}//存储到 链表容器中list.push_back(text);}}return list;
}

调用方法

//返回当前目录下以txt结尾的文件
std::list<string> list = Dir::entryList("./",".txt");

http://www.lryc.cn/news/278336.html

相关文章:

  • 【Linux】Linux系统编程——Linux目录结构
  • 卫星时钟服务器、NTP时钟服务器、GPS北斗网络时钟系统
  • 【Kafka-3.x-教程】-【三】Kafka-Broker、Kafka-Kraft
  • 全网第一篇教你怎么总结多线程知识
  • Mondo备份linux操作系统为iso镜像 —— 筑梦之路
  • 《微信小程序开发从入门到实战》学习七十八
  • React 入门 - 05(响应式与事件绑定)
  • 【APACHE】的认识和基础配置参数
  • 英飞凌TC3xx之一起认识GTM(九)GTM相关知识简述及应用实例(CMU,CCM,TBU,MON)
  • STL之list
  • 【C++】C++11中的常见语法(上)
  • Unity 踩坑记录 AnyState 切换动画执行两次
  • 使用脚本把springboot程序部署到k8s上
  • 使用valgrind 分析缓存命中
  • leetcode-二进制求和
  • 数据采集卡:16通道16位250KHz AD,支持单点采集,程控增益
  • 物联网通讯协议NB-lot和LoRa差异分析
  • 回归预测 | Matlab基于SMA+WOA+SFO-LSSVM多输入单输出回归预测
  • Redis内存策略:「过期Key删除策略」+ 「内存淘汰策略」
  • el-table 个体行绑定点击事件时 表格中有el-radio和el-checkbox 点击触发两次事件处理方法
  • CentOs 环境下使用 Docker 部署 Ruoyi-Vue
  • axios 后端不配和添加api
  • 力扣LCR 166. 珠宝的最高价值(java 动态规划)
  • 【Python基础】一文搞懂:Python 中 Excel 文件的写入与读取
  • 二叉树题目:完全二叉树插入器
  • 用MATLAB求最短路径(graphshortestpath)和求最小生成树(minspantree),代码演示
  • 用win系统搭建Minecraft世界服务器,MC开服教程,小白开服教程
  • MacOS安装Miniforge、Tensorflow、Jupyter Lab等(2024年最新)
  • iOS 应用上架指南:资料填写及提交审核
  • 车速预测 | Matlab基于RBF径向基神经网络的车速预测模型(多步预测,尾巴图)