把一个文件里的内容复制到另一个文件的问题
有两个磁盘文件a和b,要求把这两个文件中的信息合并(按字母顺序排列),输出到新文件c中去
学c的时候没学,现在饭回来学,有点问题
我这里假设a和b中存的都是英文字母
a中为abcdeeeeeefg b中为abbbdcz,复制到c后出现的是aabbbbcddceeeeeefgz空格处都是一个非法字符请指点
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fp_a,*fp_b,*fp_c;
char ch;
char a1[50],b1[50];
char *a=a1,*b=b1;
//打开a文件
if( (fp_a=fopen("a1.txt","r"))==NULL )
{
printf("cannot open file a.txt\n");
exit(0);
}
//打开b文件
if( (fp_b=fopen("b1.txt","r"))==NULL )
{
printf("cannot open file b.txt\n");
exit(0);
}
//打开c文件
if( (fp_c=fopen("c1.txt","wr"))==NULL )
{
printf("cannot open file c.txt\n");
exit(0);
}
while( !feof(fp_a) )
{
*a++=fgetc(fp_a);
}
*a='\0';
while( !feof(fp_b) )
{
*b++=fgetc(fp_b);
}
*b='\0';
a=a1;b=b1;
while( *a && *b )
{
if(*a <= *b)fputc(*a++,fp_c);
else fputc(*b++,fp_c);
}
while( *a )fputc(*a++,fp_c);
while( *b )fputc(*b++,fp_c);
//关闭文件
fclose( fp_a );
fclose( fp_b );
fclose( fp_c );
}