#include//标准输入输出#include //各种系统调用#include //与目录有关的操作#include //与字符串处理有关的函数#include //与文件状态有关#include //linux系统自定义的类型void show_directory(char * dir_name,int level);int main(int argc, char * argv[]){ if(argc != 2) { fprintf(stdout,"%s\n","参数输入有问题!"); return 1; } show_directory(argv[1],0); return 0; }/*用于显示目录 * 第一个参数为目录的名称 * 第二个参数为目录为几及目录 * */void show_directory(char * dir_name,int level){ DIR * dir; //目录流指针 struct dirent * entry; //目录项结构体指针 struct stat statbuf; if( ( dir = opendir(dir_name) ) == NULL ) return ; chdir(dir_name); char cwd[100]; getcwd(cwd,100); int dir_name_len; dir_name_len = strlen(dir_name); fprintf(stdout,"%*s文件夹中:\n",level + dir_name_len,dir_name); while( ( entry = readdir(dir) ) != NULL ) { lstat(entry->d_name,&statbuf); //如果是目录,就第归调用 if( S_ISDIR (statbuf.st_mode) ) { if( strcmp(".",entry->d_name) == 0 || strcmp("..",entry->d_name) == 0) { continue; } int level2; level2 = level + dir_name_len + 10; //计算出空格数 show_directory(entry->d_name,level2); } else if( S_ISREG(statbuf.st_mode) ) //如果是文件,输出 { int length = level + (int)strlen(entry->d_name) + dir_name_len + 10; //子段应有占的长度 fprintf(stdout,"%*s\n",level + (int)strlen(entry->d_name) + dir_name_len + 10,entry->d_name); } } chdir(".."); closedir(dir);}