C/C++的readdir和readdir_r函数(遍历目录)
1.首先要打开目录文件
DIR *opendir( const char *name);
DIR *fdopendir( int fd);
2.读取目录文件信息的函数
注意:这是个库函数
struct dirent *readdir( DIR *dirp);
int readdir_r( DIR *dirp, struct dirent *entry, struct dirent **result);
文件目录结构体:
struct dirent {ino_t d_ino; /* inode number 索引节点号*/off_t d_off; /* not an offset; see NOTES 在目录文件中的偏移*/unsigned short d_reclen; /* length of this record 文件名长*/unsigned char d_type; /*type of file; not supported by all filesystem types 文件类型*/ char d_name[256]; /* filename 文件名,最长255字符*/
};
d_type的值为:
- DT_BLK This is a block device.
- DT_CHR This is a character device.
- DT_DIR This is a directory.
- DT_FIFO This is a named pipe (FIFO).
- DT_LNK This is a symbolic link.
- DT_REG This is a regular file.
- DT_SOCK This is a UNIX domain socket.
- DT_UNKNOWN The file type is unknown.
readdir()函数实例:
注意:
每次使用readdir后,readdir会读到下一个文件,readdir是依次读出目录中的所有文件,每次只能读一个
这个特性和readdir_r()一样
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main(int argc, char **argv){DIR *pDir = NULL;struct dirent * pEnt = NULL;unsigned int cnt = 0; if (argc != 2){printf("usage: %s dirname\n", argv[0]);return -1;}pDir = opendir(argv[1]);if (NULL == pDir){perror("opendir");return -1;} while (1){pEnt = readdir(pDir);if(pEnt != NULL){if (pEnt->d_type == DT_REG){printf("是普通文件:");}else{printf("不是普通文件:");}printf("name:[%s] \n", pEnt->d_name);cnt++;}else{break;}};printf("总文件数为:%d\n", cnt);return 0;
}
结果:
$ ./a.out .
是普通文件:name:[a.c]
不是普通文件:name:[.]
不是普通文件:name:[..]
是普通文件:name:[a.out]
不是普通文件:name:[12_sr]
不是普通文件:name:[10_sr]
不是普通文件:name:[17_sr]
不是普通文件:name:[15_sr]
不是普通文件:name:[14.sr]
不是普通文件:name:[18_sr]
不是普通文件:name:[udp]
不是普通文件:name:[16_sr]
不是普通文件:name:[tcp]
总文件数为:13
C++
#include <iostream>
#include <string>
using namespace std;
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <dirent.h>void search(string commonXmlPath){
{string ectPath = commonXmlPath;string subDistrictXmlPath;DIR* dir_pointer = opendir(ectPath.c_str());if (!dir_pointer)return;for (dirent* dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)){std::string filename = dp->d_name;if (filename == "." || filename == ".."){continue;}if (dp->d_type == 8)//?录镁{int split = filename.find('.');if (split == -1)continue;string pp = ectPath + filename;cout << pp.c_str() <<"\n";}if(dp->d_type == 4)//目录拢卢?么录?酶碌梅 {//每赂枚募镁录?路戮露subDistrictXmlPath = ectPath + string("/") + dp->d_name;search(subDistrictXmlPath);}}//closedir(dir_pointer);
}
int main(){search(string("/data/home/android/configserver/common_xmls/etc"));return 0;
}
readdir_r():
注意:
这三个参数
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
int main(int argc, char **argv)
{DIR *pDir = NULL;struct dirent * pEnt = NULL;struct dirent *entry = (struct dirent *)malloc(sizeof(struct dirent));struct dirent **result = (struct dirent **)malloc(sizeof(struct dirent));unsigned int cnt = 0;unsigned int ret = 0; if (argc != 2){printf("usage: %s dirname\n", argv[0]);return -1;}pDir = opendir(argv[1]);if (NULL == pDir){perror("opendir");return -1;}ret = readdir_r(pDir , entry , result);printf("return :%d \n", ret);printf("name :[%s] \n", entry->d_name);printf("name :[%s] \n", result[0]->d_name);ret = readdir_r(pDir , entry , result);printf("return :%d \n", ret);printf("name :[%s] \n", entry->d_name);printf("name :[%s] \n", result[0]->d_name);return 0;}
结果:
C/C++的readdir和readdir_r函数(遍历目录)相关推荐
- Linux下 C 遍历目录(opendir,readdir函数)
opendir()函数: 头文件: #include <sys/types.h> #include <dirent.h> 函数原型: Dir* opendir(const ch ...
- php 遍历 目录,PHP采用自定义函数实现遍历目录下所有文件的方法
目录的遍历是PHP程序设计中经常会用到的一个功能,很多PHP项目都有这一功能模块.今天本文就来实例解析一下PHP采用自定义函数实现遍历目录下所有文件的方法.具体方法如下: 方法一:使用readir() ...
- php遍历视频文件,php使用glob函数遍历文件和目录详解
php glob()函数返回匹配指定模式的文件名或目录.因此我们可以使用glob函数来查找文件,也可以实现目录的遍历. 函数说明:array glob ( string $pattern [, int ...
- opendir、readdir和closedir函数
注意:在Linux中,目录的输入格式:/mnt//fghs./mnt/fghs./mnt/fghs和/mnt/fghs//是等效的,都一样. #include <sys/types.h> ...
- php scandir 目录树,使用php scandir函数遍历文件夹目录和所有文件
使用php scandir函数遍历文件夹目录和所有文件 $dir = "."; //当前目录 list_file($dir); function list_file($dir){ ...
- linux C 遍历目录及其子目录 opendir -> readdir -> closedir
在 linux 下遍历某一目录下内容 LINUX 下历遍目录的方法一般是这样的: 打开目录->读取->关闭目录 相关函数是 opendir -> readdir -> clos ...
- glob php,php使用glob函数遍历文件和目录详解
php glob()函数返回匹配指定模式的文件名或目录.因此我们可以使用glob函数来查找文件,也可以实现目录的遍历. 函数说明:array glob ( string $pattern [, int ...
- 函数遍历IOS中block的使用
最近研究函数遍历,稍微总结一下,以后继续补充: block是ios4.0开始推出的,其特点是可以执行内联,作为参数传递到函数,block变量的定义,^ 用于运算符. -(id)loadNibWithC ...
- react-native map函数遍历数据及其布局
react-native map函数遍历数据及其布局 当需要在react-native 中用map函数遍历带图片的数据时,小编总会发现,布局有点别扭, 效果图类似下面这样的图 我先以遍历如下数据得到产 ...
最新文章
- 算法之道:形而之上谓之道
- unix cheatsheet
- 故障转移集群仲裁盘_windows 2012 标准版 集群 仲裁盘和共享盘无法转移
- Android之访问网络,使用HttpURLConnection还是HttpClient?
- 用IIS配置反向代理
- golang之tcp自动重连
- 靠谱测试人员需要具备专业技术能力
- LNMP Keepalived Haproxy 笔记
- 【MVC】使用FormCollection获取Form表单数据
- Javascript命名禁区有哪些
- 代码整洁 vs 代码肮脏
- java 地图_java离线地图web GIS制作
- GP数据库获取指定时段的所有天数
- 老版TP-Link路由器设置固定时间上网
- ACM-ICPC 2018 北京赛区网络预赛 Tomb Raider(暴力)
- 音符和频率的对应关系计算
- 数学对计算机的重要性
- 面试中常见的50个面试题目及答案
- 基于金字塔的模板匹配算法(python)
- 【单片机毕业设计】【mcuclub-jj-013】基于单片机的衣柜的设计