文件IO练习:外部输入一个路径,要求显示该路径下,所有文件的详细信息,除了隐藏文件。

        这个题需要用到IO文件函数,调用opendir函数打开所有文件,再调用readdir来一个一个地读取文件,再将文件名传递给stat函数,返回文件的详细信息的一个结构体,然后再讲结构体中的成员依次打印即可,也可以将结构体中的成员规范化成ls -l中的格式。

        代码实现。

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <dirent.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>

#define ERR_MSG(msg) {\
	fprintf(stderr,"line: %d\n",__LINE__);\
	perror(msg);\
}
void filePermission(mode_t mode){
	for(int i = 0; i < 3; i ++){
		int num = 0400;
		num = num >> (i*3);
		int type = 1;
		int type2 = num >> 3;
		while(num > type2){
			if((mode & num) != 0){
				if(type == 1)
					printf("r");
				else if(type == 2)
					printf("w");
				else
					printf("x");
			}
			else{
				printf("-");
			}
			type ++;
			num = num >> 1;
		}
	}
}

void get_fileType(mode_t m){
	if(S_ISREG(m)){
		putchar('-');
	}
	else if(S_ISDIR(m)){
		putchar('d');
	}
	else if(S_ISCHR(m)){
		putchar('c');
	}
	else if(S_ISBLK(m)){
		putchar('b');
	}
	else if(S_ISFIFO(m)){
		putchar('p');
	}
	else if(S_ISLNK(m)){
		putchar('l');
	}
	else if(S_ISSOCK(m)){
		putchar('s');
	}
}

void get_fileTypebydefine(mode_t mode){
	switch(mode & S_IFMT){
		case S_IFSOCK: printf("s"); break;
		case S_IFLNK: printf("l"); break;
		case S_IFREG: printf("-"); break;
		case S_IFBLK: printf("b"); break;
		case S_IFDIR: printf("d"); break;
		case S_IFCHR: printf("c"); break;
		case S_IFIFO: printf("p"); break;
		default: printf("类型提取失败\n");
	}
}

void get_fileuid(uid_t id){
	struct passwd *uid = getpwuid(id);
	if(NULL == uid){
		ERR_MSG("getpwuid");
		return;
	}
	printf(" %s",uid->pw_name);
}

void get_filegid(gid_t id){
	struct group *gid = getgrgid(id);
	if(NULL == gid){
		ERR_MSG("getgrgid");
		return;
	}
	printf(" %s",gid->gr_name);
}


void get_fileMsg(char *p){
	struct stat buf;
	if(stat(p,&buf) < 0){
		ERR_MSG("stat");
		return;
	}
	get_fileTypebydefine(buf.st_mode);
	filePermission(buf.st_mode);
	printf(" %ld",buf.st_nlink);
	get_fileuid(buf.st_uid);
	get_filegid(buf.st_gid);
	printf(" %6ld",buf.st_size);
	struct tm *time = localtime(&buf.st_ctime);
	printf(" %02d-%02d %02d:%02d",\
			time->tm_mon+1,time->tm_mday,time->tm_hour,time->tm_min);	
}

int main(int argc, const char *argv[])
{
	if(argc < 2){
		printf("请输入文件名\n");
		return -1;
	}
	DIR * dp = opendir(argv[1]);
	if(NULL == dp){
		ERR_MSG("opendir");
		return -1;
	}
	struct dirent *rp = NULL;
	while(1){
		char mid[20]= "\0";
		strcpy(mid,argv[1]);
		rp = readdir(dp);
		if(NULL == rp){
			if(0 == errno){
				printf("目录读取完毕\n");
				break;
			}
			else{
				ERR_MSG("readdir");
				return -1;
			}
		}
		if(rp->d_name[0] == '.'){
			continue;
		}
		get_fileMsg(strcat(mid,rp->d_name));
		printf(" %s\n",rp->d_name);
	}
	if(closedir(dp) < 0){
		ERR_MSG("closedir");
		return -1;
	}
	return 0;
}

运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值