实现多行数据拼接到另一个txt中
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
for (int i = 1; i < 5; i++)
{
string str = "";
char line[128] = {};
char path[50] = {};
sprintf_s(path, "%s%d%s", "D:\\c++\\mytrain\\train_",i,".txt");
ifstream infile(path);
if (!infile.is_open())
{
cout << "open file error!" << endl;
}
while (infile.getline(line, sizeof(line)))
{
cout << line << endl;
str=str+line+"\n";
}
infile.close();
sprintf_s(path, "%s%d%s", "D:\\c++\\resolt\\train_",i,"_resoult.txt");
ofstream outfile(path,ios::app);
outfile<<str;
outfile.close();
}
system("pause");
return 0;
}