日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux目录统计编程,linux系统编程----统计一个目录下的普通文件个数

發布時間:2025/3/19 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux目录统计编程,linux系统编程----统计一个目录下的普通文件个数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

主要是為了統計linux系統下一個指定目錄下面的普通文件個數,運用目錄操作的一些函數,配合遞歸調用來實現該功能。

首先介紹一下函數原型:

打開一個空目錄

DIR *opendir(const char *name);

參數: 目錄名

返回值: 指向目錄的指針

讀目錄

struct dirent *readdir(DIR *dirp);

參數: opendir的返回值

返回值: 指向目錄的指針

關閉目錄

int closedir(DIR *dirp);

代碼實現:readfileNum.c

1 #include

2 #include

3 #include

4 #include

5 #include

6

7

8 int get_file_num(char*root)9 {10 int total = 0;11 DIR* dir =NULL;12 //打開目錄

13 dir =opendir(root);14 //循環從目錄中讀文件

15

16 char path[1024];17 //定義記錄目錄指針

18 struct dirent* ptr =NULL;19 while( (ptr = readdir(dir)) !=NULL)20 {21 //跳過. 和 ..

22 if(strcmp(ptr->d_name, ".") == 0 || strcmp(ptr->d_name, "..") == 0)23 {24 continue;25 }26 //判斷是不是目錄

27 if(ptr->d_type ==DT_DIR)28 {29 sprintf(path, "%s/%s", root, ptr->d_name);30 //遞歸讀目錄

31 total +=get_file_num(path);32 }33 //如果是普通文件

34 if(ptr->d_type ==DT_REG)35 {36 total ++;37 }38 }39 closedir(dir);40 returntotal;41 }42

43 int main(int argc, char*argv[])44 {45 if(argc < 2)46 {47 printf("./a.out path");48 exit(1);49 }50

51 int total = get_file_num(argv[1]);52 printf("%s has regfile number: %d\n", argv[1], total);53 return 0;54 }

效果展示:

原文:http://www.cnblogs.com/liunianshiwei/p/6045395.html

總結

以上是生活随笔為你收集整理的linux目录统计编程,linux系统编程----统计一个目录下的普通文件个数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。