2023/12/05 work

1. 使用文件IO完成对图像的读写操作

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>

int main(int argc, const char *argv[])
{
	
	if(argc !=3){
		printf("please enter picture: ");
		return -1;
	}

	//文件io操作图片
	//文件描述符
	int fp =-1;
	if(( fp = open(argv[1],O_RDWR)) ==-1){
		perror("picture open error");
		return -1;
	}

	//光标后移 2字节
	lseek(fp,2,SEEK_SET);
	//光标在后移 54字节
	lseek(fp,54,SEEK_SET);

	unsigned char color[3] = {255,0,0};
	//在文件的最后输出白色
	for(int i=0;i<65;i++){
		for(int j =0;j<1200;j++){
			write(fp,color,sizeof(color));
		}
	}

	//光标移动到最开始的位置,在开始写到另一个文件中
	lseek(fp,0,SEEK_SET);

	//生成一个新的图片
	//写的文件描述符
	int fwb = -1;
	if((fwb = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664))==-1){
		perror("picture write error");
		return -1;
	}

	//循环读
	int count = 0;
	char buf[10]="";
	while((count = read(fp,buf,sizeof(buf))) != 0){
		write(fwb,buf,count);
	}

	close(fp);
	close(fwb);

	return 0;
}

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <myhead.h>

int main(int argc, const char *argv[])
{

	//if(argc != 2){
	//	printf("please enter file");
	//	return -1;
	//}

	//先得到目录下所有文件
	DIR *dp = NULL;
	if((dp = opendir("./")) == NULL){
		perror("opendir error");
		return -1;
	}

	struct dirent *rp =NULL;

	while((rp = readdir(dp)) != NULL){

		//stat 获取文件信息
		struct stat bu;
		if(stat(rp->d_name,&bu) == -1){
			perror("stat file error");
			return -1;
		}

		printf("file type %#o\n",bu.st_mode&S_IFMT);
		printf("file auth %#o\n",bu.st_mode&0777);
		printf("file inode %ld\n",bu.st_ino);
		printf("file dev %ld\n",bu.st_dev);
		printf("file blksize %ld\n",bu.st_blksize);
		printf("file blocks %ld\n",bu.st_blocks);
		printf("file size %ld\n",bu.st_size);
		printf("file rdev %ld\n",bu.st_rdev);
		printf("file nlink %ld\n",bu.st_nlink);
		printf("file uid %d\n",bu.st_uid);
		printf("file gid %d\n",bu.st_gid);
		printf("file atim  %ld\n",bu.st_atim);
		printf("file name %s\n",rp->d_name);
		printf("--------------------------------------------\n");
	}
	return 0;
}

3. 思维导图

  • 11
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值