CP命令的实现

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

#define BUFSIZE 10

void oops(char *s1, char *s2)
{
	fprintf(stderr,"Error: %s ",s1);
	perror(s2);
	exit(1);
}

int main(int argc, char *argv[])
{
	char Buf[BUFSIZE];
	int Infd, Outfd,nChars;

	if(3 != argc)
	{
		fprintf(stderr,"usage : %s source destination\n"
,*argv);
		exit(1);
	}
	//参数1是要创建的文件
	if(-1 == (Outfd = creat(argv[1],0744)))
		oops("Create Fail",argv[1]);
	//参数2是要读取的文件
	if(-1 == (Infd = open(argv[2],O_RDONLY)))
		oops("Open Fail",argv[2]);
	
	//只要可以读取到数据
	while((nChars = read(Infd,Buf,BUFSIZE)) > 0)
	{
		//判断是否写入的和读取的大小相同
		if(write(Outfd,Buf,nChars) != nChars)
		{
			oops("Write Error",argv[1]);
		}
	}

	if(-1 == nChars)
		oops("Read Error From",argv[2]);
	//关闭文件描述符
	if(-1 == close(Infd)||-1 ==  close(Outfd))
		oops("Error Closing File","");

	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值