欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > c/c++ >内容正文

c/c++

C/C++的readdir和readdir_r函数(遍历目录)

发布时间:2025/3/15 c/c++ 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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函数(遍历目录)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。