一个文件的小程序

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<process.h>



int main(int argc,char *argv[])
{
	FILE *fp1;
	FILE *fp2;
	char ch1;
	char ch2;
	int i=1;
	int j=1;
	
	if((fp1=fopen(argv[1],"r"))==NULL)
	{
		printf("Can't open file %s.\n",argv[1]);
		exit(EXIT_FAILURE);
	}
	if((fp2=fopen(argv[2],"r"))==NULL)
	{
		printf("Can't open file %s.\n",argv[2]);
		exit(EXIT_FAILURE);
	}
	ch1=getc(fp1);
	ch2=getc(fp2);
	while((ch1!=EOF)||(ch2!=EOF))
	{
		while((ch1!=EOF)&&(ch1!='\n'))
		{
			putc(ch1,stdout);
			ch1=getc(fp1);
		}
		if(ch1!=EOF)
		{
			putchar('\n');
			ch1=getc(fp1);	
		}
		if((ch1==EOF)&&(i==1)&&(j==1))//juge which file is first to get to the end.The fist one shule put a '\n' in the end and the other one need not to
		{
			putchar('\n');
			i++;
			j++;
		}
		while((ch2!=EOF)&&(ch2!='\n'))
		{
			putc(ch2,stdout);
			ch2=getc(fp2);
		}
		if(ch2!=EOF)
		{
			putchar('\n');
			ch2=getc(fp2);	
		}
		if((ch1==EOF)&&(j==1)&&(i==1))//juge which file is first to get to the end.The fist one shule put a '\n' in the end and the other one need not to
		{
			putchar('\n');
			j++;
			i++;
		}
	}
	if(fclose(fp1)!=0)
		printf("Can't close the file %s\n",argv[1]);
	if(fclose(fp2)!=0)
		printf("Can't close the file %s\n",argv[2]);
	exit(EXIT_SUCCESS);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值