我在编译我编写的这段代码时遇到问题。此代码旨在通读两个文本文件,然后输出这两个文件中的行。然后,我希望能够放置两个文件并将它们组合起来,但是 file1 文本在第一行,而 file2 文本在之后的行上。
这是我的代码:
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
std::ifstream file1("file1.txt");
std::ifstream file2("file2.txt");
//std::ofstream combinedfile("combinedfile.txt");
//combinedfile << file1.rdbuf() << file2.rdbuf();
char filename[400];
string line;
string line2;
cout << "Enter name of file 1(including .txt): ";
cin >> filename;
file1.open(filename);
cout << "Enter name of file 2 (including .txt): ";
cin >> filename;
file2.open(filename);
if (file1.is_open())
{
while (file1.good() )
{
getline (filename,line);
cout << line << endl;
}
file1.close();
}
else cout << "Unable to open file";
return 0;
}
if (file2.is_open())
{
while (file2.good() )
{
getline (filename,line);
cout << line << endl;
}
file2.close();
}
else cout << "Unable to open file";
return 0;}