C++多个TXT文件合并成一个TXT文件

编写一个C++程序,要求合并多个TXT文件为一个TXT文件。例如输入的文件为:1.txt,2.txt,3.txt,4.txt输出的文件为:5.txt要求:合并之后各文件的行数保持不变,各个文件的数据不要头尾相连!数据保持原来的格式!谢谢大家!运行成功后,我会追加悬赏的!

#include<iostream>
using namespace std;int main(){
FILE *txt_file1 = fopen("text1.txt","r");
FILE *txt_file2 = fopen("text2.txt","r");
FILE *txt_file3 = fopen("text3.txt","r");
FILE *txt_file4 = fopen("text4.txt","r");
FILE *txt_file5 = fopen("text5.txt","w+");
char ch=' ';
char ch1='\n';
while((fread(&ch,1,sizeof(char),txt_file1) )){
fwrite(&ch,1,1,txt_file5);
}
fwrite(&ch1,1,1,txt_file5); while((fread(&ch,1,sizeof(char),txt_file2) )){
fwrite(&ch,1,1,txt_file5);
}
fwrite(&ch1,1,1,txt_file5); while((fread(&ch,1,sizeof(char),txt_file3) )){
fwrite(&ch,1,1,txt_file5);
}
fwrite(&ch1,1,1,txt_file5); while((fread(&ch,1,sizeof(char),txt_file4) )){
fwrite(&ch,1,1,txt_file5);
}
fwrite(&ch1,1,1,txt_file5);

return 0;
}可以这样简单的写
温馨提示:答案为网友推荐,仅供参考
相似回答