C语言文件怎样合并?

如题所述

#include <stdio.h>
const int MAX_SIZE_PER_LINE = 1024;

int main(int argc, char *argv[])
{
FILE * file_a,*file_b,*file_c;
char buffer[MAX_SIZE_PER_LINE*2];
file_a = fopen("a.txt","r");
file_b = fopen("b.txt","r");
file_c = fopen("c.txt","w");
while(NULL != fgets(buffer ,MAX_SIZE_PER_LINE,file_a))
{
//不是最后一行肯定会有换行符,消除这个换行符
if('\n' == buffer[strlen(buffer)-1])
fgets(buffer+strlen(buffer)-1 ,MAX_SIZE_PER_LINE,file_b);
//对于最后一行可能没有换行符的情况特殊处理
else
fgets(buffer+strlen(buffer) ,MAX_SIZE_PER_LINE,file_b);
fputs(buffer,file_c);
}
fclose(file_a);
fclose(file_b);
fclose(file_c);
return 0;
}

/*
A.txt :
-------
aa*
bb*
cc*
dd*
ee*
ff*
gg*

B.txt :
-------
11
22
33
44
55
66
77

C.txt :
-------
aa*11
bb*22
cc*33
dd*44
ee*55
ff*66
gg*77

说明: A.txt 和 B.txt 要放在可执行程序同一个目录下。C.txt 会自动在这个目录下生成。
*/
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网