#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
//C=AUB
void MergeFile(const char *pathA,const char *pathB,const char *pathC)
{
FILE *fr1 = fopen(pathA,“r”);
FILE *fr2 = fopen(pathB,“r”);
FILE *fw = fopen(pathC,“w”);
assert(fr1!=NULL && fr2!=NULL && fw!=NULL);
char tmp1;
char tmp2;
int len1 = fread(&tmp1,sizeof(char),1,fr1);
int len2 = fread(&tmp2,sizeof(char),1,fr2);
while(len1>0 && len2>0)//两个文件都还有数据
{
if(tmp1 <= tmp2)
{
fwrite(&tmp1,sizeof(char),1,fw);
len1 = fread(&tmp1,sizeof(char),1,fr1);
}
else
{
fwrite(&tmp2,sizeof(char),1,fw);
len2 = fread(&tmp2,sizeof(char),1,fr2);
}
}
//一个文件数据完成,另一个还有数据
while(len1 > 0)
{
fwrite(&tmp1,sizeof(char),1,fw);
len1 = fread(&tmp1,sizeof(char),1,fr1);
}
while(len2 > 0)
{
fwrite(&tmp2,sizeof(char),1,fw);
len2 = fread(&tmp2,sizeof(char),1,fr2);
}
fclose(fr1);
fclose(fr2);
fclose(fw);
}
int main()
{
char *path1 = “a.txt”;
char *path2 = “b.txt”;
char *path3 = “c.txt”;
MergeFile(path1,path2,path3);
return 0;
}