王杰IOday4

#include 
#include 
#include 	
#include 
int main(int argc, const char *argv[])
{
	int f=open(argv[1],O_RDWR);
	if(f<0)
	{
		ERR_MSG("open");
		return -1;
	}
	off_t size=lseek(f,0,SEEK_END);
	lseek(f,0,SEEK_SET);
	int p=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0664);
	
	char buf;
	ssize_t res=0;
	int count=0;

	pid_t cpid=fork();
	if(cpid>0)
	{
		while(1)
		{
			bzero(&buf,sizeof(buf));
			res=read(f,&buf,sizeof(buf));
			count+=res;
			write(p,&buf,res);
			if(size/2==count)
				break;
		}
	}
	else if(0==cpid)
	{
		sleep(1);
		while(1)
		{
			bzero(&buf,sizeof(buf));
			res=read(f,&buf,sizeof(buf));
			if(0==res)
				break;
			write(p,&buf,res);
		}
	}


	close(f);
	close(p);
	return 0;
}

王杰IOday4_第1张图片

你可能感兴趣的:(c语言)