#include "head.h"
int main(int argc, const char *argv[])
{
	
	int op = open("/*YOUR PHOTO PWD*/",O_RDONLY);
	int cp = open("/*YOUR CP PHOTO PWD*/",O_WRONLY | O_CREAT | O_TRUNC,0777);
	
	long size = lseek(op,0,SEEK_END);
	
	long pos = size/2;
	
	char c;
	int i = 0;
 
	int fk = fork();
	if( fk >0 ){
		//父进程,拷贝前一半
		wait(NULL);
		printf("SEEK1 = %ld\n",lseek(op,0,SEEK_SET));
		printf("SEEK2 = %ld\n",lseek(cp,0,SEEK_SET));
		while( i < pos ){
			read(op,&c,1);
			write(cp,&c,1);
			i++;
		}
		printf("copy1 success\n");
	}
	else if( fk == 0 ){
		//子进程,拷贝后一半
		printf("SEEK1 = %ld\n",lseek(op,pos,SEEK_SET));
		printf("SEEK2 = %ld\n",lseek(cp,pos,SEEK_SET));
		while( i < pos ){
			read(op,&c,1);
			write(cp,&c,1);
			i++;
		}
		printf("copy2 success\n");
	}
	else
		ERR_MSG("fork");
 
	close(op);
	close(cp);
	return 0;
}

运行效果:

文件IO操作实现文件拷贝_IO操作

文件IO操作实现文件拷贝_IO操作_02