友链
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#include <sys/stat.h> #include <dirent.h> #include <sys/types.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int get_file_num(const char *path); int main(int argc, char *argv[]) { if (argc < 2) { printf("usage: nt%s pathn", argv[0]); return -1; } int total_file_num = get_file_num(argv[1]); printf("普通文件的个数为:%dn", total_file_num); return 0; } // 递归,用于获取目录下所有普通文件的个数 int get_file_num(const char *path) { // 打开目录 DIR *dir = opendir(path); if (dir == NULL) { perror("opendir"); exit(0); } struct dirent *ptr; // 判断是文件还是目录 // 记录普通文件的个数 int total_file_num = 0; while ((ptr = readdir(dir)) != NULL) { // 获取名称 char *d_name = ptr->d_name; // 忽略掉.和.. if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) continue; // 判断是普通文件还是目录 if (ptr->d_type == DT_DIR) { char new_path[256]; // 将path和新的目录拼接起来 sprintf(new_path, "%s/%s", path, d_name); // 继续读取这个目录 total_file_num += get_file_num(new_path); } // 普通文件 if (ptr->d_type == DT_REG) { // 继续读取这个目录 total_file_num++; } } closedir(dir); return total_file_num; }
最后
以上就是动听小猫咪最近收集整理的关于C语言统计指定目录下的普通文件个数友链的全部内容,更多相关C语言统计指定目录下内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复