其他分享
首页 > 其他分享> > 132-有两个磁盘文件“A”和“B

132-有两个磁盘文件“A”和“B

作者:互联网

//132-有两个磁盘文件"A”和"B“,各存放一行字母,今要求把这两个文件中的信息合并,输出到一个新文件”C中去
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
	FILE *fp1,*fp2,*fp3;
	fp1=fopen("A.txt","r");
	fp2=fopen("B.txt","r");
	fp3=fopen("C.txt","w");
	if(fp1==NULL || fp2 == NULL || fp3== NULL)
	{
		printf("cannot file!");
		exit(0);
	}
	
	//其实我还是不懂如何存取字母
	//分别定义两个数组
	//合并是不是有一个函数 
	
	char ch1[100],ch2[20];
	fgets(ch1,100,fp1);
	//int len1=strlen(ch1);
	fgets(ch2,20,fp2);
	//int len2=strlen(ch2);
	strcat(ch1,ch2);
	int len = strlen(ch1);
	
	char temp;
	int i,j;
	
	//排序
	for(i=0;i<len-1;i++)
	{
		for(j=i+1;j<len;j++)
		{
			if(ch1[i]>ch1[j])
			{
				temp = ch1[i];
				ch1[i] = ch1[j];
				ch1[j] = temp;	
			}
		}
	} 
	
	//输出
	for(i=0;i<len;i++)
	{
		fputc(ch1[i],fp3);
		putchar(ch1[i]);
	} 
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	return 0;
	 
} 

在这里插入图片描述

标签:文件,ch1,int,132,ch2,fp1,fp3,fp2,磁盘
来源: https://blog.csdn.net/weixin_44522477/article/details/122024195