本人初学,代码略粗糙,勿喷。
#include<stdio.h>
#include<sys/types.h>
#include<string.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
#define SIZE 1024
int main(int argc , char **argv)
{
int i=0;
int j=0;
int fd1,fd2,fd3,nread1,nread2,nwrite;
char buf1[SIZE];
char buf2[SIZE];
char *p1, *p2;
fd1=open(argv[1],O_RDONLY);
if(fd1==-1)
{
printf("fail open f1");
}
fd2=open(argv[2],O_RDWR | O_CREAT ,0644);
if(fd2==-1)
{
printf("fail open f2");
}
while(1)
{
nread1=read(fd1,buf1,SIZE);
p1=buf1;
if(nread==-1)
{
printf("read error");
break;
}
if(nread==0)
{
printf("read ok");
break;
}
nread2= read(fd2,buf2,SIZE);
p2=buf2;
for(i=nread2;i<(nread1+nread2);i++)
{ if(j<nread1)
{
*(p2+i)=*(p1+j);
}
j++;
}
close(fd2);//这里要先关掉在打开,实现fd2文件的清空
fd3=open(argv[2],O_RDWR|O_CREAT|O_TRUNC,0644);
nwrite=write(fd3,buf2,(nread2+nread1));
close(fd1);
close(fd3);
return 0;
}
}