#include<head.h>
int main(int argc, const char *argv[])
{
pid_t pid=fork();
if(pid<0)
{
perror("fork失败\n");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
FILE *a=fopen("1.txt","r");
FILE *b=fopen("2.txt","w");
if(a==NULL)
{
perror("打开失败");
exit(EXIT_FAILURE);
}
char buf[1024];
size_t n;
while((n=fread(buf,1,sizeof(buf),a))>0)
{
if(fwrite(buf,1,n,b)!=n)
{
perror("写入失败\n");
exit(EXIT_FAILURE);
}
}
fclose(a);
fclose(b);
printf("拷贝成功\n");
}
else
{
FILE *a=fopen("3.txx","r");
FILE *b=fopen("4.txt","w");
if(a==NULL||b==NULL)
{
perror("打开失败\n");
exit(EXIT_FAILURE);
}
char buffer[1024];
size_t n;
while(n=fread(buffer,1,sizeof(buffer),a)>0)
{
if(fwrite(buffer,1,n,b)!=n)
{
perror("写入失败\n");
exit(EXIT_FAILURE);
}
}
fclose(a);
fclose(b);
printf("拷贝成功\n");
wait(NULL);
}
return 0;
}