实验要求:
对【程序5_9】进行扩展,要求参数为目录名,且其下至少有三层目录,分别用深度遍历及广度遍历两种方法对此目录进行遍历,输出此目录下所有文件的大小及修改时间。
一.题目名称
文件系统扩展实验
二.问题描述
分别使用深度遍历和广度遍历两种方法对目录进行遍历,然后输出在目录下的所有文件,以及他们的修改时间,切目录至少有三层。
三.问题分析
深度遍历:就是每次搜索一个文件夹,当遇到文件夹时,继续搜索此文件夹,当一直往下到没有搜索的文件夹时,返回上一层搜索。遇到普通文件就输出文件大小和最后修改的时间,并返回上一层。
广度遍历:每次遍历完同一层次的所有文件,遇到普通文件输出文件的大小和最后的修改时间,遍历完以后再进入下一层,全部遍历完遂结束。
四.解决方案
DFS:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
struct stat statbuf;
struct dirent* direntp;
DIR* dirp;
char dirbuf[512];
// 递归遍历目录和文件
static void dfs(const char* filename)
{
char tempbuf[512];
DIR* tempdirp;
// 复制文件名到临时缓冲区
strcpy(tempbuf, filename);
// 读取文件状态,若读取失败,输出错误信息
if (stat(tempbuf, &statbuf) == -1) {
printf("获取 %s 的状态失败:%s\n", tempbuf, strerror(errno));
return;
}
// 判断是否是目录文件
if (S_ISDIR(statbuf.st_mode)) {
// 如果是目录文件,打开目录,并将打开的目录文件信息存储在 tempdirp 中
if ((tempdirp = opendir(tempbuf)) == NULL) {
// 目录打开失败,输出错误信息并退出
printf("打开目录 %s 失败:%s\n", tempbuf, strerror(errno));
exit(1);
}
char iterbuf[512]; // 用于存放子目录名
// 读取目录中的文件和子目录
while ((direntp = readdir(tempdirp)) != NULL) {
// 如果是“..”或“.”需要跳过
if (strcmp(direntp->d_name, "..") == 0 || strcmp(direntp->d_name, ".") == 0)
continue;
// 生成子目录路径
memset(iterbuf, 0, sizeof(iterbuf));
strcpy(iterbuf, tempbuf);
strcat(iterbuf, "/");
strcat(iterbuf, direntp->d_name);
// 复制生成的子目录路径到全局缓冲区
memset(dirbuf, 0, sizeof(dirbuf));
strcpy(dirbuf, iterbuf);
// 递归遍历子目录
dfs(dirbuf);
}
closedir(tempdirp); // 关闭目录
} else if (S_ISREG(statbuf.st_mode)) {
// 判断是否是普通文件
printf("%s\t 大小:%ld 字节\t修改时间:%s", tempbuf, statbuf.st_size, ctime(&statbuf.st_mtime));
}
}
int main(int argc, char *argv[])
{
if (argc != 2) {
// 输出用法信息
printf("用法:%s 文件名\n\a", argv[0]);
exit(1);
}
// 调用递归函数遍历目录和文件
dfs(argv[1]);
closedir(dirp); // 关闭目录
return 0;
}
BFS:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <time.h>
char path[1024];
struct dirent *direntp;
DIR *dirp;
// 获取文件大小和最后修改时间
static int get_file_size_time(const char *filename) {
struct stat stabuf;
// 获取filename的stat
if (stat(filename, &stabuf) == -1) {
printf("获取文件状态失败:%s\n", filename);
return -1;
}
// 判断是否为目录文件
if (S_ISDIR(stabuf.st_mode))
return 1;
// 是否为普通文件
if (S_ISREG(stabuf.st_mode))
printf("%s:大小 %ld 字节\t修改时间:%s", filename, stabuf.st_size, ctime(&stabuf.st_mtime));
// 输出文件的大小和最后修改时间
return 0;
}
// 广度优先搜索遍历文件系统
void bfs(const char *name) {
// 初始化队列,队列保存每个目录的状态
char queue[1000][1024];
int front = 0, rear = 0;
strcpy(queue[rear++], name);
// 距离数组,记录每个目录的深度
int depth[1000] = {0};
while (front < rear) {
// 取出队头
const char *t = queue[front++];
// 打开目录的信息放置dirp中
if ((dirp = opendir(t)) == NULL) {
printf("打开目录失败:%s\n", t);
exit(1);
}
while ((direntp = readdir(dirp)) != NULL) {
strcpy(path, t);
strcat(path, "/");
strcat(path, direntp->d_name);
int ans = get_file_size_time(path);
if (ans == -1) {
break;
} else if (ans == 1) {
if ((strcmp(direntp->d_name, ".") != 0) && (strcmp(direntp->d_name, "..") != 0)) {
strcpy(queue[rear], path);
rear++;
depth[rear] = depth[front] + 1; // 更新深度
}
}
}
closedir(dirp);
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "用法:%s 目标路径\n", argv[0]);
exit(1);
}
bfs(argv[1]);
exit(1);
}
五.实验结果
文件结构:
DFS:
BFS: