获取某个目录下特定格式文件列表
来源:互联网 发布:吉他调音软件app 编辑:程序博客网 时间:2024/05/07 21:48
1. 有时为了获取某个目录下特定后缀名文件,是非常有必要的,下面是提取指定后缀的代码
//以下代码支持windows平台,其它平台如mac后续增加
//file_filtor.h
#ifndef FILE_FILTOR_H#defineFILE_FILTOR_H#ifdef__cplusplusextern "C" {#endiftypedef struct TargetFile TargetFile;struct TargetFile{ char* path; TargetFile* next; TargetFile* previous;};TargetFile *GetFileFromDir(const char *dir, const char *postfix);void DelTargetFileList(TargetFile *target);#ifdef__cplusplus}#endif#endif/* FILE_FILTOR_H */
//file_filtor.cpp
#include <dirent.h>#include <iostream>#include <stdlib.h>#include <string.h>#include <string>#include "file_filtor.h"TargetFile *GetFileFromDir(const char *path, const char *postfix){ DIR *dir = NULL; dir = opendir(path); if (!dir) { return NULL; } std::string str_file_dir = path; if (str_file_dir[str_file_dir.size()-1] != '/') { str_file_dir.append("/"); } TargetFile *head = (TargetFile *)malloc(sizeof(TargetFile)); memset(head, 0, sizeof(TargetFile)); TargetFile *last_file = head; while(-1 != dir->dd_stat) { dirent *file_dir; file_dir = readdir(dir); if (!file_dir || (!strcmp(file_dir->d_name, ".")) || (!strcmp(file_dir->d_name, ".."))) { continue; } if (_A_SUBDIR == dir->dd_dta.attrib) { //是目录就继续循环 continue; } //获取后缀 std::string str_postfix = file_dir->d_name; std::string::size_type pos = str_postfix.find_last_of("."); if (std::string::npos == pos) { continue; } str_postfix = str_postfix.substr(pos); if (str_postfix != postfix) { continue; } TargetFile *cur_file = (TargetFile *)malloc(sizeof(TargetFile)); memset(cur_file, 0, sizeof(TargetFile)); std::string str_file_path = str_file_dir + file_dir->d_name; cur_file->path = (char *)malloc(str_file_path.size() + 1); strcpy(cur_file->path, str_file_path.c_str()); cur_file->path[str_file_path.size()] = '\0'; cur_file->previous = last_file; last_file->next = cur_file; last_file = cur_file; } //end while closedir(dir); //去除head结点 TargetFile *ret = head->next; ret->previous = NULL; free(head); return ret;}void DelTargetFileList(TargetFile *target){ if (!target) { return ; } TargetFile *next = target; while (next) { TargetFile *cur = next; next = next->next; if (cur->path) { free(cur->path); } }}
//例子
//main.cpp
/* * Simple C++ Test Suite */#include "file_filtor.h"int main(int argc, char** argv) { TargetFile *ret = GetFileFromDir("D:/Users/Desktop/resource/pdf", ".pdf"); TargetFile *next = ret; while(next) { std::cout<<next->path<<std::endl; next = next->next; } DelTargetFileList(ret); return 0;}
- 获取某个目录下特定格式文件列表
- 获取某个目录下特定格式文件列表[2]_mac版本
- C#学习之获取某个文件下的目录列表及子目录列表
- linux 在某个目录下递归查找特定字符串
- 如何用 tmpwatch 删除某个目录下的特定文件
- 获取某个目录下的文件名称
- 获取某个目录下所有的文件
- 获取特定的目录
- 使用Python处理目录(二):计算某个目录下特定文件类型中文件总行数
- bash获取特定目录下所有文件的全路径
- Java递归获取目录下所有特定后缀文件
- Linux C语言获取目录下特定类型的文件名
- Windows下在某个目录下的所有文件中查找特定字符串
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- 获取指定目录下的文件列表
- 获取指定目录下的文件名列表
- Collection:List、SetMap:HashMap、HashTable
- Chrome 23 下 调试 JS
- Android OpenGL ES 开发教程(15):通用的矩阵变换指令
- 序列化总结
- 统计用户下的表的大小排行(包含大字段统计)
- 获取某个目录下特定格式文件列表
- (三)Activity入门指南
- QT进程间通信 详细介绍
- 同时运行两个TOMCAT的配置
- Oracle10g RAC srvctl libpthread.so.0
- hadoop学习笔记之二:MapReduce基本编程
- Json 语法 格式
- centOS开机取消图形界面,直接进入文本模式
- Hadoop学习笔记之三:用MRUnit做单元测试