作业:用read write 拷贝文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{
if(argc!=3)
{perror("参数不足");
exit(-1);}
int fp1,fp2;
fp1=open(argv[1],O_RDONLY);//打开要被拷贝的文件
if(fp1<0)
{
perror("fp1 open fail");
exit(-1);
}
fp2=open(argv[2],O_RDWR|O_CREAT,0664);//打开目标文件
if(fp2<0)
{
perror("fp2 open fail");
exit(-1);
}
char buf[50];//读写是的缓存容器
long flen=0;//文件长度
int ret=1;
while(ret)
{
ret=read(fp1,buf,sizeof(buf));//要拷贝的读入缓存
if(ret<0)
{
perror("read fail");
exit(-1);
}
write(fp2,buf,sizeof(buf));//缓存内容写入目标文件
flen=flen+ret;
bzero(buf,sizeof(buf));//清理缓存容器
}
printf("there are %ld chars\n",flen);
close(fp2);
close(fp1);
return 0;
}