c++ 函数-遍历文件夹
#pragma once #include <iostream> #include <string.h> #include <unistd.h> #include <sys/io.h> #include <sys/types.h> #include <sys/stat.h> #include "dirent.h" #include <time.h> #include <string> #include <vector> #include <algorithm> using namespace std; #define MICRO_IN_SECOND 1000000 #define NANOS_IN_SECOND 1000000000 // 获取文件后缀名 static string getExtension(string filename) { return filename.substr(filename.find_last_of('.')); } // 判断是不是图片 static bool is_image(string filename) { string ext = getExtension(filename); vector<string> formats = {".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp"}; vector<string>::iterator it = find(formats.begin(), formats.end(), ext); return it != formats.end(); } // 获取文件名,不包含目录 static string getFilename(string file_fullpath) { return file_fullpath.substr(file_fullpath.find_last_of('/') + 1); } //获取文件名,不包含目录和后缀名 static string getFilenameNoExt(string filename) { filename = getFilename(filename); return filename.substr(0, filename.find_last_of('.')); } static string getParentPath(string file_fullpath) { int idx = file_fullpath.find_last_of('/'); return file_fullpath.substr(0, idx); } //读取某给定路径下所有文件夹与文件名称,并带完整路径 static void getAllFilesInDirs(string path, vector<string> &fileFullpath) { DIR *pDir; struct dirent *ptr; if (!(pDir = opendir(path.c_str()))) return; while ((ptr = readdir(pDir)) != 0) { if (ptr->d_type == DT_DIR) { if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { string new_path = path + "/" + ptr->d_name; getAllFilesInDirs(new_path, fileFullpath); } } else { fileFullpath.push_back(path + "/" + ptr->d_name); } } closedir(pDir); } //读取某给定路径下后缀名为format得文件名称,并带完整路径 static void getAllFilesByformat(string path, vector<string> &fileFullpath, string format) { DIR *pDir; struct dirent *ptr; string f; if (!(pDir = opendir(path.c_str()))) return; while ((ptr = readdir(pDir)) != 0) { if (ptr->d_type == DT_DIR) { if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) { string new_path = path + "/" + ptr->d_name; getAllFilesByformat(new_path, fileFullpath, format); } } else if (ptr->d_type == DT_REG && getExtension(ptr->d_name) == format) { f.clear(); f = path + string("/") + string(ptr->d_name); fileFullpath.push_back(f); } } closedir(pDir); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
2018-11-09 mysql-8.0.13在windows上的部署
2018-11-09 爬虫 - 动态分页抓取 游民星空 的资讯 - bs4
2018-11-09 爬虫下载QQ音乐:获取所有歌手-每个歌手的专辑-每个专辑里的歌曲
2018-11-09 爬虫下载校花网美女信息-lxml