It seems I don't understand completely reading from and into files, line by line.
Here is my code that is supposed to read lines of two file, into string variables, I assume, and then write them into third file:
line of first file
line of second file
.... so on
if one of files ends, the rest of the other is added to the end
Where is my mistakes? it compiled, but did not write anything into third file
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main () {
string name1, name2, line1, line2, name3;
cin>>name1;
cin>>name2;
name3=name1+"."+name2;
ifstream firstFile (name1.c_str());
ifstream secondFile (name2.c_str());
ofstream thirdFile (name3.c_str());
while (!firstFile.fail() && !secondFile.fail()) {
if (firstFile.eof()){
getline (secondFile, line2);
thirdFile<<line2<<endl;
} else if (secondFile.eof()) {
getline (firstFile, line1);
thirdFile<<line1<<endl;
} else {
getline (firstFile, line1);
getline (secondFile, line2);
thirdFile<<line1<<endl<<line2<<endl;
}
}
firstFile.close();
secondFile.close();
thirdFile.close();
return 0;
}