使用C语言查看一个文件夹中所有文件及目录
1.前言
1.1聲明
文章中的文字可能存在語法錯(cuò)語以及標(biāo)點(diǎn)錯(cuò)誤,請(qǐng)諒解;
如果在文章中發(fā)現(xiàn)代碼錯(cuò)誤或其它問題請(qǐng)告知,感謝!
2.實(shí)現(xiàn)方法
為了使用C語言實(shí)現(xiàn)查看一個(gè)文件夾中的文件數(shù)量功能,可以使用dirent庫,該庫文件包含的許多UNIX系統(tǒng)服務(wù)的函數(shù)原型,例如opendir函數(shù)、readdir函數(shù),可以實(shí)現(xiàn)查看目錄中文件數(shù)量等功能。
2.1dirent中常用數(shù)據(jù)類型以及函數(shù)功能簡(jiǎn)介
下面對(duì)dirent中常用的數(shù)據(jù)類型以及函數(shù)進(jìn)行簡(jiǎn)單的解釋:
常用結(jié)構(gòu)體:
DIR結(jié)構(gòu)體:
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結(jié)構(gòu)體類似于FILE,可以使用該結(jié)構(gòu)體保存正在被讀取目錄的有關(guān)信息,例如打開目錄函數(shù)DIR *opendir(const char *FilePath), 該函數(shù)返回值為指向DIR結(jié)構(gòu)的指針(句柄),通過該指針?biāo)赶虻腄IR類型的結(jié)構(gòu)體,可以得到該目錄相關(guān)信息。
dirent的結(jié)構(gòu)體:
struct dirent {long d_ino; /* inode number索引節(jié)點(diǎn)號(hào) 在<sys/types.h>文件該類型的描述*/off_t d_off; /* offset to this dirent 在目錄文件中的偏移*/unsigned short d_reclen; /*Length of this d_name 文件名長(zhǎng) */unsigned char d_type; /*the type of d_name 文件類型*/char d_name[NAME_MAX+1]; /*file name (null-terminated) 文件名,最長(zhǎng)256字符 */} }一般該結(jié)構(gòu)體配合readdir函數(shù)使用,也就是通過readdir函數(shù)獲取到的文件屬性保存到該結(jié)構(gòu)體中。?
需要注意的是,對(duì)于dirent的結(jié)構(gòu)體,我們經(jīng)常能用到該結(jié)構(gòu)體中d_ name 變量, 另外 ,short d_reclen是指這個(gè)文件的文件名長(zhǎng)度,而不是文件大小。當(dāng)然,如果要獲得該文件大小,可以使用sizeof ( d _name)實(shí)現(xiàn)。?
(當(dāng)然,如果我們想要得到該文件的更多信息,可以使用stat函數(shù)實(shí)現(xiàn),具體用法可以參見下面的博客:?
https://blog.csdn.net/farmwang/article/details/77996187)
通過上述介紹,可以簡(jiǎn)單概括dirent結(jié)構(gòu)體和DIR結(jié)構(gòu)體的區(qū)別:dirent結(jié)構(gòu)體保存的是文件屬性,DIR結(jié)構(gòu)體保存的目錄屬性。
常用庫函數(shù):
DIR *opendir(const char);
通過打開一個(gè)目錄路徑(const char)返回一個(gè)DIR類型的指針,后續(xù)對(duì)該目錄的讀取和搜索操作都使用該指針。
dirent *readdir(DIR *);
返回DIR中的目錄或者文件實(shí)體指針,類型為dirent,需要注意的是,當(dāng)該函數(shù)再一次被調(diào)用的時(shí)候,該dirent指針指向下一個(gè)目錄或者文件實(shí)體。當(dāng)函數(shù)返回NULL時(shí),表明指針已經(jīng)指向目錄的結(jié)尾。
void seekdir(DIR *dir, long int offset);
用來設(shè)置目錄操作流的讀取位置,調(diào)用seekdir函數(shù)的時(shí)候便從該位置進(jìn)行讀取。參數(shù)offset表示相對(duì)于文件目錄開頭的偏移量。
int telldir(DIR *dir);
返回當(dāng)前目錄流中指針位置。該位置是相對(duì)于文件開頭偏移量的下一個(gè)實(shí)體讀取位置的返回值。
2.2函數(shù)實(shí)現(xiàn)
2.2.1實(shí)現(xiàn)邏輯
實(shí)現(xiàn)方法一般為:
1.打開目錄(opendir());
2.讀取文件(readdir());
3.關(guān)閉目錄(closedir).
2.2.2函數(shù)例子
#include <stdio.h> #include "dirent.h>#define FilePath "/opt/test"int main() {int filesize = 0;DIR *dir = NULL;struct dirent *entry;if ((dir = opendir(FilePath)) == NULL) {printf("opendir failed!");return -1;} else {while (entry = readdir(dir)) {printf("filename%d = %s", i, entry->d_name); //輸出文件或者目錄的名稱printf("filetype = %d\n", entry->d_type); //輸出文件類型}closedir(dir);}return 0; }運(yùn)行結(jié)果如下:
以上。?
參考文檔:?
1.https://blog.csdn.net/wangshubo1989/article/details/52994327?
2.https://blog.csdn.net/dream_allday/article/details/75243818?
3.https://blog.csdn.net/qq_20916555/article/details/51132642?
4.http://www.360doc.com/content/15/0701/10/5470123_481878714.shtml
?
不過,在實(shí)際的業(yè)務(wù)中,我用的是apr庫的較上一層的api,因?yàn)槿绻孟到y(tǒng)的api還需要考慮平臺(tái)的問題。
?
?
轉(zhuǎn)載地址:https://blog.csdn.net/wangqingchuan92/article/details/80109793
?
?
總結(jié)
以上是生活随笔為你收集整理的使用C语言查看一个文件夹中所有文件及目录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Json Schema快速入门
- 下一篇: Linux内存管理初探