<< endl will flush the buffer and add a '\n'
You can think of it was being equivalent to adding a '\n' at the end, for added simplicity.
If you want to never add a newline just write the code as:
ifstream read1("1.txt");
ifstream read2("2.txt");
ofstream write ("3.txt");
string line;
string line2;
while ( getline ( read1, line, '\n' ) )
{
if (!line.empty())
write << line;
}
while ( getline ( read2, line2, '\n' ) )
{
if (!line2.empty())
write << line2;
}
read1.close();
read2.close();
write.close();
If you wish to write a newline after every line in the fact except the last line of the last file than buffer the thing into a string and write that to the file... not the most efficient option maybe, but good enough:
ifstream read1("1.txt");
ifstream read2("2.txt");
ofstream write ("3.txt");
string line;
string line2;
string buffer
while ( getline ( read1, line, '\n' ) )
{
if (!line.empty())
buffer += line;
buffer += "\n";
}
while ( getline ( read2, line2, '\n' ) )
{
if (!line2.empty())
buffer += line2;
buffer += "\n";
}
//Remove the last char from buffer (the '\n' bothering you)
buffer.substr(0, myString.size()-1)
write << buffer;
read1.close();
read2.close();
write.close();
Marek Vitek's answer is actually better than mine:
ofstream write ("3.txt");
string line;
string line2;
while ( getline ( read1, line, '\n' ) )
{
if (!line.empty())
write << line << endl;
}
while ( getline ( read2, line2, '\n' ) )
{
if (!line2.empty()) {
write << line2
}
if(!line2.empty() && !read2.eof() {
write << line2 << endl;
}
}
read1.close();
read2.close();
write.close();