C一百题

有两个磁盘文件A和B,各存放一行字母,要求把
这两个文件中的信息合并(按字母顺序排列),输出
到一个新文件C中。

源码:


#include

int main(int argc, char *argv[])
{
     
	FILE *fp;
	int i,j,n,ni;
	char c[160],t,ch;

	if((fp = fopen("A","r")) == NULL)
	{
     
		printf("file A cannot be opened\n");
	}

	printf("\n A contents are :\n");

	for(i=0;(ch=fgetc(fp) )!= EOF && ch != '\n';i++)
	{
     
			c[i] = ch;
			putchar(c[i]);
		
	}

	fclose(fp);

	n=i;
	if((fp = fopen("B","r")) == NULL)
	{
     
		printf("file B cannot be open\n");
	}

	printf("\n B contents are : \n");
	for( i = n;(ch=fgetc(fp))!= EOF&& ch != '\n';i++)
	{
     
			c[i] = ch;
			putchar(c[i]);
	}
	fclose(fp);

	n = i;
	for(i=0;i<n;i++)
	{
     
		for(j = i+1;j<n;j++)
		{
     
			if(c[i]>c[j])
			{
     
				t = c[i];
				c[i] = c[j];
				c[j] = t;
			}
		}
	}

	printf("\n C file is :\n");
	fp = fopen("C","w");
	for(i = 0;i<n;i++)
	{
     
		putc(c[i],fp);
		putchar(c[i]);
	}
	fclose(fp);
	printf("\n");
    return 0;
}

你可能感兴趣的:(C)