我有一個問題,使這段代碼我寫了編譯。此代碼旨在通讀兩個文本文件,然後在這兩個文件中輸出該行。然後,我希望能夠放置兩個文件併合並它們,但是文件1的文本在第一行,文件2的文本在這之後。閱讀兩個文本文件,然後將它們合併
這裏是我的代碼:
所有的#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;}
編譯器說什麼? – HAL