经过多次调试、和对比目前树状图尚为发现bug。 下面是系统中的树状图和我的树状图的对比 以下是源码 tree.c
#include "tree.h" int main(int argc, char **argv) { if (argc != 2) { printf ("请输入你要tree的目录\n"); return -1; } //打印第一个目录 printf (GREEN "%s\n", argv[1]); //结构体的初始化 TREE_BUG tree_bug; bzero (&tree_bug, sizeof (TREE_BUG)); tree_bug.noend == 1; //默认未到达文件末尾 tree_bug.noend_buf[flg_buf_arg] = tree_bug.noend; // tree_bug.buf[0] = 1 cycle_read_tree_dir (argv[1], &tree_bug); //将目录的初始化的结构体传进去 } bool cycle_read_tree_dir (char *tree_dir, TREE_BUG *tree_bug) { DIR *open_value = opendir (tree_dir); //打开要读取的目录 if (open_value == NULL) { fprintf (stderr, "opendir %s failed:%s\n", tree_dir, strerror (errno)); return true; } //获取传进来目录的文件数 int file_num = get_filenum_in_the_dir(tree_dir); int i; int present_num = 0; //记录已读取到的文件数 struct dirent *read_value; //准备结构体 传递给下一个函数 TREE_BUG next_tree_bug; bzero (&next_tree_bug, sizeof (TREE_BUG)); next_tree_bug.noend = 1; next_tree_bug.noend_buf[flg_buf_arg] = next_tree_bug.noend; for (i = 0; i < tree_deep; i++) //将上一个嵌套函数的数组的值传递给下一个 传递的个数为树深 { next_tree_bug.noend_buf[i] = tree_bug->noend_buf[i]; } //递归读取目录 while(1) { read_value = readdir(open_value); //读取完跳出循环 if (read_value == NULL) break; //跳过隐藏文件 if (read_value->d_type == DT_DIR && (strcmp (".", read_value->d_name) == 0 || \ strcmp ("..", read_value->d_name) == 0)) continue; //如果是目录 递归读取 if (read_value->d_type == DT_DIR) { present_num ++; //更新已读取到的文件数 //读取数到达目录末尾 if (present_num == file_num) { next_tree_bug.noend = 0; } //更新数组 到达文件末尾时 将对于数组对于位置置0 next_tree_bug.noend_buf[flg_buf_arg] = next_tree_bug.noend; for (i = 0; i < tree_deep; i++) { if (tree_bug->noend_buf[i] == 0) //判断前面的几层数深是否需要打印竖线 { printf (NONE" "); continue; } printf (NONE"| "); } printf (NONE"|___"); printf (GREEN "%s\n", read_value->d_name); char buf[256]; bzero (buf, 256); sprintf (buf, "%s/%s", tree_dir, read_value->d_name); tree_deep ++; //树深加一 flg_buf_arg ++; //进入下一层数深 数组中的"i"加1 记录下一层数是否到达文件末尾 //进入下一层数 cycle_read_tree_dir (buf, &next_tree_bug); //退出一层数 tree_deep --; //树深减一 flg_buf_arg --; //数组中的"i"减1 } //如果不是目录 同上 else { present_num ++; if (present_num == file_num) { next_tree_bug.noend = 0; } next_tree_bug.noend_buf[flg_buf_arg] = next_tree_bug.noend; for (i = 0; i < tree_deep; i++) { if (tree_bug->noend_buf[i] == 0) { printf (NONE" "); continue; } printf (NONE"| "); } printf (NONE "|___%s\n", read_value->d_name); } } } //获取文件数 int get_filenum_in_the_dir(char *dirname) { int num = 0; DIR *open_value = opendir (dirname); //打开要读取的目录 if (open_value == NULL) { fprintf (stderr, "opendir %s failed:%s\n", dirname, strerror (errno)); return true; } struct dirent *read_value; //递归读取目录 while(1) { read_value = readdir(open_value); if (read_value == NULL) break; //跳过隐藏文件 if (read_value->d_type == DT_DIR && (strcmp (".", read_value->d_name) == 0 || \ strcmp ("..", read_value->d_name) == 0)) continue; if (read_value->d_type == DT_REG || read_value->d_type == DT_DIR) num++; } return num; }以下是头文件tree.h
#ifndef _TREE_H_ #define _TREE_H_ #include "myhead.h" #define GREEN "\e[0;32m" #define NONE "\e[0m" int tree_deep; //数深 int flg_buf_arg; //相当于noend_buf[i]中的i typedef struct tree_bug //定义一个结构体解决数的bug { int noend_buf[20]; //用来判断有多少数层到达文件末尾 并记录下来 int noend; //到达文件末尾的标志位 0表示到达文件末尾 1表示未到达文件末尾 }TREE_BUG; bool cycle_read_tree_dir (char *tree_dir, TREE_BUG *tree_bug); int get_filenum_in_the_dir(char *dirname); //获取目录的文件数 #endif