C/C++的readdir和readdir_r函数(遍历目录)
生活随笔
收集整理的這篇文章主要介紹了
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] 總文件數為:13C++
#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函数(遍历目录)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 获取Shell(提权)后的操作(wind
- 下一篇: ubuntu下pip的安装、升级和使用