有两个磁盘文件A和B,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件C中去...
#include<stdio.h>
#define SIZE 100
int main()
{
FILE *pFileA,*pFileB,*pFileC;
char ch,temp,array[SIZE];
int i = 0,j=0,k=0;
//文件A,B路径可以随便写,打开时记得路径就可以了
if ( (pFileA = fopen("A.txt","a+")) == NULL)
{
printf("Cannot open file strike any key exit!");
}
if ( (pFileB = fopen("B.txt","r")) == NULL)
{
printf("Cannot open file strike any key exit!");
}
/*把B的内容放到A*/
ch = fgetc(pFileB);
while ( ch != EOF )
{
fputc(ch,pFileA);
ch = fgetc(pFileB);
}
fclose(pFileA);
fclose(pFileB);
if ( (pFileA = fopen("A.txt","a+")) == NULL)
{
printf("Cannot open file strike any key exit!");
}
/*把A的内容放到缓冲数组*/
ch = fgetc(pFileA);
while ( ch != EOF)
{
array[i] = ch;
ch = fgetc(pFileA);
i++;
}
array[i] = '\0';
/*冒泡排序*/
for ( j=0; j<i-3;j++ )
{
for ( k=j+1;k<i-1;k++ )
{
if( array[j] > array[k] )
{
temp = array[j];
array[j] = array[k];
array[k] = temp;
}
}
}
// printf("%s",array);
if ( (pFileC = fopen("C.txt","w")) == NULL)
{
printf("Cannot open file strike any key exit!");
}
/*把缓冲数组内容写到C*/
for( j=0; j<i-1; j++ )
{
fputc(array[j],pFileC);
}
return 0;
}