1、编写一个程序用于访问操作系统中某个目录下的所有文件,递归打印出所有文件的名称、类型(目录、普通文件、链接文件等)、文件大小,程序运行以传递运行命令参数的形式去设置需要遍历的目录,操作参考如下,需要用到<unistd.h>中的getopt函数: 2、能够按照文件大小进行分类展示,比如按照小于10KB为一组显示、10KB~100KB为一组显示等大小分类展示。
时间: 2024-03-07 20:47:40 浏览: 167
好的,下面是一个实现上述功能的C语言程序。程序会递归地遍历指定目录下的所有文件,并输出文件的名称、类型、大小。并且可以按照文件大小对文件进行分类展示。程序中使用了`getopt`函数来处理命令行参数,使用`stat`函数获取文件属性信息,使用递归进行目录遍历。
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
void print_file_info(char *filename, struct stat *filestat) {
printf("%-40s ", filename);
if (S_ISDIR(filestat->st_mode)) {
printf("Dir ");
} else if (S_ISREG(filestat->st_mode)) {
printf("Regular file");
} else if (S_ISLNK(filestat->st_mode)) {
printf("Link file ");
} else {
printf("Unknown ");
}
printf("%10ld bytes\n", filestat->st_size);
}
void traverse_dir(char *dirname, int size_group) {
DIR *dir;
struct dirent *entry;
struct stat filestat;
char path[1024];
int size_kb;
if ((dir = opendir(dirname)) == NULL) {
fprintf(stderr, "Cannot open directory: %s\n", dirname);
return;
}
while ((entry = readdir(dir)) != NULL) {
if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) {
continue;
}
snprintf(path, sizeof(path), "%s/%s", dirname, entry->d_name);
if (lstat(path, &filestat) < 0) {
fprintf(stderr, "Cannot stat file: %s\n", path);
continue;
}
if (S_ISDIR(filestat.st_mode)) {
traverse_dir(path, size_group);
} else {
size_kb = (int)(filestat.st_size / 1024);
if (size_group < 0 || (size_kb >= size_group && size_kb < size_group + 10)) {
print_file_info(path, &filestat);
}
}
}
closedir(dir);
}
int main(int argc, char *argv[]) {
int opt, size_group = -1;
while ((opt = getopt(argc, argv, "s:")) != -1) {
switch (opt) {
case 's':
size_group = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-s size_group] directory\n", argv[0]);
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
fprintf(stderr, "Usage: %s [-s size_group] directory\n", argv[0]);
exit(EXIT_FAILURE);
}
traverse_dir(argv[optind], size_group);
return 0;
}
```
使用示例:
```
$ ./traverse_dir -s 0 /usr/share
/usr/share/applications/ibus-setup.desktop Regular file 1937 bytes
/usr/share/applications/mimeinfo.cache Regular file 341131 bytes
/usr/share/applications/mimeinfo.cache.old Regular file 341131 bytes
/usr/share/applications/session-properties.desktop Regular file 1777 bytes
/usr/share/applications/ubuntu-amazon-default.desktop Regular file 1451 bytes
/usr/share/applications/ubuntu-amazon-default.desktop.dpkg-dist Regular file 1451 bytes
/usr/share/applications/unity-control-center.desktop Regular file 2325 bytes
/usr/share/applications/unity-control-center.desktop.dpkg-old Regular file 2325 bytes
/usr/share/applications/unity-control-center.desktop.dpkg-dist Regular file 2325 bytes
/usr/share/applications/unity-settings-daemon.desktop Regular file 1904 bytes
/usr/share/applications/unity-settings-daemon.desktop.dpkg-dist Regular file 1904 bytes
/usr/share/applications/unity-webapps-hybris.desktop Regular file 1462 bytes
/usr/share/applications/unity-webapps-hybris.desktop.dpkg-dist Regular file 1462 bytes
...
```
在上述示例中,`-s 0`表示不按照文件大小分类展示;`-s n`表示按照小于n KB的文件为一组,n~n+10 KB的文件为一组,依次类推展示。
阅读全文