8月18日作业

思维导图

 作业一

#include<myhead.h>

int main(int argc, const char *argv[])
{
	//定义源文件
	FILE *fp_src;
	if((fp_src=fopen("test.txt","r")) == NULL){
		perror("fopen error");
		return -1;
	}
	//定义目标文件
	FILE *fp_des;
	if((fp_des=fopen("copy.txt","w")) == NULL){
		perror("fopen error");
		return -1;
	}
	//求出源文件长度,赋值给len
	fseek(fp_src,0,SEEK_END);
	int len;
	len = ftell(fp_src);
	int len1 = len - len/2;
	printf("len = %d\n",len);

	pid_t pid;
	pid = fork();//创建子进程

	char buf[len/2]; //定义容器存一半的文件
	if(pid < 0){
		perror("fork error");
		return -1;
	}else if(pid == 0){
		//子进程复制后半
		fclose(fp_des);
		if((fp_des=fopen("copy.txt","w")) == NULL){
			perror("fopen error");
			return -1;
		}
		fseek(fp_src,len/2,SEEK_SET);//将光标定位到文件1/2处
		fread(buf, 1, len1, fp_src); //读取后半文件
		fwrite(buf, 1, len1, fp_des);//写入后半文件
		printf("子进程结束\n");
		exit(EXIT_SUCCESS);
	}else{
		wait(NULL); //等待子进程
		fclose(fp_des);
		if((fp_des=fopen("copy.txt","r")) == NULL){
			perror("fopen error");
			return -1;
		}
		char buf1[len/2];
		fread(buf1, 1, len1, fp_des); //读取已写入的后半段文件
		fclose(fp_des);
		if((fp_des=fopen("copy.txt","w")) == NULL){
			perror("fopen error");
			return -1;
		}
		fseek(fp_src,0,SEEK_SET); 	
		fread(buf, 1, len/2, fp_src); //读取前半文件

		fwrite(buf, 1, len/2, fp_des);//写入前半文件
		fseek(fp_src,len/2,SEEK_SET); 	
		fwrite(buf1, 1, len1, fp_des);//写入后半文件

	}
	return 0;
}

 作业二

#include<myhead.h>

int main(int argc, const char *argv[])
{
	//定义源文件
	FILE *fp_src;
	if((fp_src=fopen("test.txt","r")) == NULL){
		perror("fopen error");
		return -1;
	}
	//定义目标文件
	FILE *fp_des;
	if((fp_des=fopen("copy.txt","w")) == NULL){
		perror("fopen error");
		return -1;
	}
	//求出源文件长度,赋值给len
	fseek(fp_src,0,SEEK_END);
	int len;
	len = ftell(fp_src);
	int len1 = len - len/3 - len/3; //后1/3的长度
	printf("len = %d\n",len);

	pid_t pid1;
	pid1 = fork();//创建子进程1

	char buf[len/3]; //定义容器存1/3的文件
	if(pid1 < 0){
		perror("fork error");
		return -1;
	}else if(pid1 == 0){
		//子进程1复制中间1/3
		fclose(fp_des);
		if((fp_des=fopen("copy.txt","w")) == NULL){
			perror("fopen error");
			return -1;
		}
		fseek(fp_src,len/3,SEEK_SET);//将光标定位到文件1/3处
		fread(buf, 1, len/3, fp_src); //读取中间1/3文件
		fwrite(buf, 1, len/3, fp_des);//写入中间1/3文件
		printf("子进程1结束\n");
		exit(EXIT_SUCCESS);
	}else{
		pid_t pid2;
		pid2 = fork(); //创建子进程2
		if(pid2 < 0){
			perror("fork error");
			return -1;
		}else if(pid2 == 0){
			//子进程2复制后1/3
			fclose(fp_des);
			if((fp_des=fopen("copy.txt","w")) == NULL){
				perror("fopen error");
				return -1;
			}
			fseek(fp_src,len/3,SEEK_SET);//将光标定位到文件1/3处
			fseek(fp_src,len/3,SEEK_CUR);//将光标定位到文件2/3处
			fread(buf, 1, len1, fp_src); //读取后1/3文件
			fwrite(buf, 1, len1, fp_des);//写入后1/3文件
			printf("子进程2结束\n");
			exit(EXIT_SUCCESS);
		}else{

			waitpid(pid2, NULL, 0); //等待子进程2结束
			fclose(fp_des);
			if((fp_des=fopen("copy.txt","r")) == NULL){
				perror("fopen error");
				return -1;
			}
			char buf2[len/3];
			fread(buf2, 1, len1, fp_des); //读取并保存已写入的后1/3文件

			waitpid(pid1, NULL, 0); //等待子进程1结束
			fclose(fp_des);
			if((fp_des=fopen("copy.txt","r")) == NULL){
				perror("fopen error");
				return -1;
			}
			char buf1[len/3];
			fread(buf1, 1, len/3, fp_des); //读取并保存已写入中间的1/3文件

			fclose(fp_des);
			if((fp_des=fopen("copy.txt","w")) == NULL){
				perror("fopen error");
				return -1;
			}
			fseek(fp_src,0,SEEK_SET); 	
			fread(buf, 1, len/3, fp_src); //读取前1/3文件

			fwrite(buf, 1, len/3, fp_des);//写入前1/3文件
			fseek(fp_src,len/3,SEEK_SET); 	
			fwrite(buf1, 1, len/3, fp_des);//写入中间1/3文件
			fseek(fp_src,len/3,SEEK_CUR); 	
			fwrite(buf2, 1, len1, fp_des);//写入后1/3文件
		}
		return 0;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值