将两个文件合并成一个文件出错,小白求指教

要回杭州的李同学 2018-01-03 10:00:54
#include<iostream>
#include<fstream>
using namespace std;

int main()
{
int i = 1;
char a[1000], b[1000], c[1000], d[1000];

fstream file1("1.txt", ios::in | ios::out | ios::trunc);
fstream file2("2.txt", ios::in | ios::out | ios::trunc);
ofstream ofile("3.txt", ios::out | ios::app);

cout << "输入两行字符串分别存至file1和file2: " << endl;

cin.getline(a, 999);
file1 << a << endl;
while(!file1.eof())
{
file1.getline(c, 999);
ofile << c;
}
file1.close();

cin.getline(b, 999);
file2 << b << endl;
while(!file2.eof())
{
file2.getline(d, 999);
ofile << d;
}
file2.close();

ofile.close();

char tmp;
ifstream ifile3("3.txt", ios::in);
while(!ifile3.eof())
{
tmp = ifile3.get();
cout << tmp;
}
ifile3.close();
cout << endl;

system("pause");
return 0;
}
...全文
271 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
ios::trunc If the file already exists, its contents are discarded. This mode is implied if ios::out is specified, and ios::ate, ios::app, and ios:in are not specified. 不能用ios::trunc这种模式啊。 这种模式打开文件会丢弃文件内容的。
赵4老师 2018-01-04
  • 打赏
  • 举报
回复
system("copy /b file1+file2 file12");

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧