IO学习 day 2

使用文件io函数实现文件拷贝

#include <head.h>
int main(int argc, const char *argv[])
{
	int fd=open("file",O_RDONLY);
	if(fd==-1)
	{
		ERR_MSG("open error!");
	}
	printf("文件1打开成功!\n");
	umask(0);
	int fd1=open("file1",O_CREAT|O_WRONLY|O_TRUNC,0777);
	if(fd1==-1)
	{
		ERR_MSG("open error!");
	}
	printf("文件2打开成功!\n");
	char s[128]="";
	while(read(fd,s,sizeof(s)-1)>0)
	{
		if(write(fd1,s,strlen(s))==-1)
		{
			ERR_MSG("write error");
		}
		printf("%s",s);
	}

	printf("拷贝成功!\n");

	if (close(fd)==-1)
	{
		ERR_MSG("close error");
	}
	printf("文件1关闭成功!\n");
	if (close(fd1)==-1)
	{
		ERR_MSG("close error");

	}
	printf("文件2关闭成功!\n");
	return 0;
}

运行结果

用标准io函数实现图片拷贝

#include <head.h>
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("1","r");
	if(fp==NULL)
	{
		ERR_MSG("fopen error");
	}
	printf("打开图片1成功!\n");
	FILE *fp1=fopen("2","w");
	if(fp1==NULL)
	{
		ERR_MSG("fopen error");
	}
	printf("打开文件成功!\n");

	char s[1024];

	while(fread(s,1,sizeof(s),fp)>0)
	{

		if(fwrite(s,1,sizeof(s),fp1)==-1)
		{
			return -1;
		}
	}
	printf("拷贝成功!\n");

	if(fclose(fp)==-1)
	{
		ERR_MSG("fclose error");
	}
	printf("关闭成功!\n");
	if(fclose(fp1)==-1)
	{
		ERR_MSG("fclose error");
	}
	printf("关闭成功!\n");

	return 0;
}

运行结果

使用文件io函数,计算文件的大小

#include <head.h>
int main(int argc, const char *argv[])
{
	int fd=open("file",O_RDONLY);
	if(fd==EOF)
	{
		ERR_MSG("open error");
	}
	printf("打开文件成功!\n");
	int set=lseek(fd,0,SEEK_END);
	printf("%d\n",set);
	if(close(fd)==-1)
	{
		ERR_MSG("close error");
	}
	printf("关闭成功!\n");
	return 0;
}

运行结果

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值