作业--day24

文章详细介绍了如何使用C语言中的文件I/O操作进行图像读写,以及如何模仿`ls-l`命令的功能,包括文件类型、权限、链接数、用户和组名等信息的处理。

使用文件IO对图像进行读写操作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
	//以只读方式打开文件./img.bmp
	int fd = -1;
	fd = open("./img.bmp", O_RDONLY);
	if(fd == -1){
		perror("open ./img.bmp error");
		return -1;
	}
	
	//以只写方式打开文件./cp_img.bmp
	int cp_fd = -1;
	cp_fd = open("./cp_img.bmp", O_RDWR | O_CREAT | O_TRUNC, 0664);
	if(cp_fd == -1){
		perror("open ./cp_img.bmp error");
		return -1;
	}

	//复制图片
	char buf[10];
	int res;
	while((res = read(fd, buf, sizeof(buf))) > 0){
		write(cp_fd, buf, res);
	}

	//获取图像的像素宽和高
	lseek(cp_fd, 18, SEEK_SET);
	unsigned int weight;
	unsigned int high;
	read(cp_fd, &weight, sizeof(weight));
	read(cp_fd, &high, sizeof(high));
	
	//将光标跳转到图像像素信息
	lseek(cp_fd, 54, SEEK_SET);

	//将1/3图像打白条
	unsigned char pix[3] = {255, 255, 255};
	int i=0, j=0;
	for(i=0; i<(high)/3; i++){
		for(j=0; j<weight; j++){
			write(cp_fd, pix, sizeof(pix));
		}
	}

	//关闭文件
	close(fd);
	close(cp_fd);
	return 0;
}

在这里插入图片描述

使用stat函数实现 ls -l 指令功能

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

void print_filetype(struct stat fstat);
void print_privilege(struct stat fstat);
void print_user_name(struct stat fstat);
void print_group_name(struct stat fstat);
void print_time(struct stat fstat);

int main(int argc, const char *argv[])
{
	if(argc != 2){
		printf("参数输入错误");
		return -1;
	}

	DIR *dp = NULL;
	dp = opendir(argv[1]);
	if(dp == NULL){
		perror("opendir error");
		return -1;
	}

	struct dirent *fp = NULL;
	char filepath[128];
	struct stat fstat;

	while((fp = readdir(dp)) != NULL){
		if(fp->d_name[0] == '.'){
			continue;
		}
		int res = snprintf(filepath, sizeof(filepath), "%s%s", argv[1], fp->d_name);
		stat(filepath, &fstat);

		//输出文件类型
		print_filetype(fstat);
		//输出文件权限
		print_privilege(fstat);
		//输出文件链接数
		printf(" %ld", fstat.st_nlink);
		//输出文件所属用户名
		print_user_name(fstat);
		//输出文件所属用户组名
		print_group_name(fstat);
		//输出文件大小
		printf(" %ld", fstat.st_size);
		//输出文件日期
		print_time(fstat);
		//输出文件名
		printf(" %s", fp->d_name);
		putchar(10);

	}
	return 0;
}

void print_filetype(struct stat fstat){

	switch(fstat.st_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;
	}
}

void print_privilege(struct stat fstat){
	int n = 8;
	while(n>=0){
		if(fstat.st_mode & 1<<n){
			switch(n%3){
				case 2:
					printf("%c", 'r');
					break;
				case 1:
					printf("%c", 'w');
					break;
				case 0:
					printf("%c", 'x');
			}
		}else{
			printf("%c", '-');
		}
		n--;
	}
}

void print_user_name(struct stat fstat){
	printf(" %s", getpwuid(fstat.st_uid)->pw_name);
}

void print_group_name(struct stat fstat){
	printf(" %s", getgrgid(fstat.st_gid)->gr_name);
}
void print_time(struct stat fstat){
	struct tm *tp = NULL;
	tp = localtime(&fstat.st_ctime);
	switch(tp->tm_mon + 1){
		case 1:
			printf(" 一");
			break;
		case 2:
			printf(" 二");
			break;
		case 3:
			printf(" 三");
			break;
		case 4:
			printf(" 四");
			break;
		case 5:
			printf(" 五");
			break;
		case 6:
			printf(" 六");
			break;
		case 7:
			printf(" 七");
			break;
		case 8:
			printf(" 八");
			break;
		case 9:
			printf(" 九");
			break;
		case 10:
			printf(" 十");
			break;
		case 11:
			printf(" 十一");
			break;
		case 12:
			printf(" 十二");
	}
	printf(" %2d %2d:%-2d", tp->tm_mday, tp->tm_hour, tp->tm_min);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值