DIR和dirent 用来获取某文件夹目录内容的结构体
2016-11-23 16:59
197 查看
DIR结构体:
函数 DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针,而该指针由以下几个函数使用:
struct dirent *readdir(DIR *dp);//读取到的文件名存储在结构体dirent的d_name成员中
void rewinddir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp);
void seekdir(DIR *dp,long loc);
dirent结构体:
代码实例:
获取二级目录下所有与.png文件同名的.pb文件列表
struct __dirstream { void *__fd; char *__data; int __entry_data; char *__ptr; int __entry_ptr; size_t __allocation; size_t __size; __libc_lock_define (, __lock) }; typedef struct __dirstream DIR;
函数 DIR *opendir(const char *pathname),即打开文件目录,返回的就是指向DIR结构体的指针,而该指针由以下几个函数使用:
struct dirent *readdir(DIR *dp);//读取到的文件名存储在结构体dirent的d_name成员中
void rewinddir(DIR *dp);
int closedir(DIR *dp);
long telldir(DIR *dp);
void seekdir(DIR *dp,long loc);
dirent结构体:
#include <dirent.h> struct dirent { long d_ino; /* inode number 索引节点号 */ off_t d_off; /* offset to this dirent 在目录文件中的偏移 */ unsigned short d_reclen; /* length of this d_name 文件名长 */ unsigned char d_type; /* the type of d_name 文件类型 */ char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长256字符 */ }
代码实例:
获取二级目录下所有与.png文件同名的.pb文件列表
void readInputFileList(std::string in_dir, std::vector<std::string> &file_list) { DIR *dir = NULL; if ((dir = opendir(in_dir.c_str())) == NULL) std::cout << in_dir << ": Open dir failed." << std::endl; struct dirent *ptr; while (ptr = readdir(dir)) { if (ptr->d_name[0] == '.') continue; else if (ptr->d_type == DT_DIR) { std::string path = in_dir + '/'; path += ptr->d_name; DIR *d = NULL; if ((d = opendir(path.c_str())) == NULL) std::cout << path << ": Open dir failed." << std::endl; struct dirent *p; while (p = readdir(d)) { if (p->d_name[0] == '.' || p->d_type == DT_DIR) continue; std::string image_name(p->d_name); if (image_name.substr(image_name.size() - 4) != ".png") continue; std::string pb_name = image_name.substr(0, image_name.size() - 3) + "pb"; std::string pb_path = path + '/' + pb_name; if (access(pb_path.c_str(), 0) != 0) continue; file_list.push_back(pb_path); } } else std::cout << ptr->d_name << ": Can not contain file." << std::endl; } sort(file_list.begin(), file_list.end()); }
相关文章推荐
- dirent和DIR 结构体 --- 表示文件夹中目录内容信息
- dirent和DIR 结构体 --- 表示文件夹中目录内容信息
- dirent和DIR 结构体 --- 表示文件夹中目录内容信息
- linux目录编程:DIR,stat, dirent结构体解析与应用
- linux目录相关函数opendir、readdir、rewinddir、closedir、telldir、seekdir、stat函数和DIR、dirent、stat结构体详解
- linux下c++判断文件夹空,DIR,dirent,stat等结构体详解
- Atitit 遍历文件夹目录解决方案与规范 attilax总结 1. 规范 2 1.1. 注意的不要同时改变文件夹内容,增删文件。这样获取到的目录list不会变化 2 1.2. 主义中文名称文件读写
- Linux下目录文件的操作(opendir,readdir,closedir) 以及DIR,dirent,stat等结构体详解
- 获取目录下的文件数和文件夹数
- VC 获取系统特殊文件夹的路径如:系统目录,桌面等
- vbs获取目录下的文件和文件夹集合
- VC 获取系统特殊文件夹的路径如:系统目录,桌面等
- scandir 读取特定目录的数据 表头文件:#include 定义函数:int scandir(const char *dir, struct dirent **namelist, nt (*select) (const struct dirent
- VC 中获取文件夹目录路径
- Asp.Net_文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(二)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)(一)
- 文件磁盘相关函数[9]-获取当前文件夹 GetCurrentDir
- 用shell获取目录/文件夹/文件的时间戳
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)