c语言实现tree的功能

写了个函数实现tree的功能,把所有的目录一目了然的呈现在屏幕上,不用一个个的去点击查看。
之前误传的是测试的代码,现在这个略有改动。
头文件还是常用的那几个

#ifndef MYIOHEAD_H_
#define MYIOHEAD_H_
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <dirent.h>
#include <linux/input.h>
// #include "kernel_list.h"
#include <termio.h>
#include <bits/signum.h>
#include <signal.h>
#include <semaphore.h>

#endif

下面是主代码,逻辑和变量搞清楚就很简单。
此处和上一篇博客差不多,暂时就不多写注释了(晚点可能会写),重点是变量和逻辑要搞清楚。

#include "myiohead.h"
int dirnum = 1;
int filenum = 0;
int theone = 0;

#define GREEN                "\e[0;32m" //give you some green collor see see
#define NONE                  "\e[0m"  //don't give you collor

//读取目录数目
int  dir_num(char *mydir)
{
	char allpath[1000];
	char otherpath[1000];
	char * dirpath = mydir;
	DIR * dir = opendir(dirpath);
	if (NULL == dir)
	{
		perror("打开目录失败!");
		exit(0);
	}
	
	struct dirent * rd = NULL;
	static int mystatic = -1;
	mystatic++;
	while((rd = readdir(dir)) != NULL) 
	{ 
		if(strncmp(rd->d_name,".",1)==0)
			continue;
		//printf("d_name : %s\n", rd->d_name); 
		// int a = mystatic;
		if (rd->d_type == DT_REG)
	 	{	
	 		// 获得文件的绝对路径
			bzero(allpath,1000);
			sprintf(allpath,"%s/%s",dirpath,rd->d_name);
			// printf("当前读取的文件名字是:%s\n",allpath);
			
			filenum++;

	 		//printf("这是普通文件!\n");
	 	}

	 	if (rd->d_type == DT_DIR)
	 	{
	 		//递归调用自己,接着读取子目录
			bzero(otherpath,1000);
			sprintf(otherpath,"%s/%s",dirpath,rd->d_name);
			dirnum++;
			dir_num(otherpath);
	 		// printf("这是目录!\n");
	 	}
	 	
	}
	return 0;
}

void read_dir(char *mydir)
{
	char allpath[1000];
	char otherpath[1000];
	char * dirpath = mydir;
	DIR * dir = opendir(dirpath);
	if (NULL == dir)
	{
		perror("打开目录失败!");
		exit(0);
	}
	
	struct dirent * rd = NULL;
	static int mystatic = 0;
	while((rd = readdir(dir)) != NULL) 
	{ 

		if(strncmp(rd->d_name,".",1)==0)
			continue;
		//printf("d_name : %s\n", rd->d_name); 
		// int a = mystatic;
		if (rd->d_type == DT_REG)
	 	{	
	 		// 获得文件的绝对路径
			bzero(allpath,1000);
			sprintf(allpath,"%s/%s",dirpath,rd->d_name);
			// printf("当前读取的文件名字是:%s\n",allpath);
			if (mystatic != 0)
			{
				printf(NONE"|");
			}
			
	 		for (int i = 0; i < mystatic; i++)
			{
				printf(NONE"   ");
			}
			printf(NONE"|_");
			printf(NONE"%s\n" ,rd->d_name);

	 		//printf("这是普通文件!\n");
	 		// if (strstr(rd->d_name,".bmp") != 0)
	 		// {
	 		// 	insert_(rd->d_name,head);
	 		// }
	 	}

	 	if (rd->d_type == DT_DIR)
	 	{
	 		//递归调用自己,接着读取子目录
			bzero(otherpath,1000);
			sprintf(otherpath,"%s/%s",dirpath,rd->d_name);
			mystatic++;
			if (mystatic>0)
			{
				printf(NONE"|");
			}
			for (int i = 0; i < mystatic-1; i++)
			{
				printf(GREEN"——————");
			}
			printf(GREEN"|_");
			printf(GREEN"%s\n" ,rd->d_name);
			
			read_dir(otherpath);
			mystatic--;
	 		// printf("这是目录!\n");
	 	}
	 	
	}

}

int main(int argc, char  *argv[])
{
	if (argc !=2)
	{
		perror("用法错误,正确用法:./a.out 目录名");
		exit(0);
	}
	printf(GREEN"%s\n", argv[1]);
	dir_num(argv[1]);
	read_dir(argv[1]);
	printf("目录数目:%d   文件数目:%d\n",dirnum,filenum);

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值