Concatenating files, extra line between their contents

I am trying to concatenate two files. All is well except for an extra line in the new file between the contents of the linked files.

File 1 contents:
1
2
f1,line 1.
f1,line 2.


File 2:
1
2
f2,line 1.
f2,line 2.


new_file:
1
2
3
4
5
f1,line 1.
f1,line 2.

f2,line 1.
f2,line 2.


Complete code below. How do I get rid of the extra line in new_file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

void append_file( string a, string b)
{
    ifstream ist{a, ios::in };
    ofstream ost{b, ios::out | ios::app };
    while(ist)
    {
        string s;
        getline( ist, s, '\n');
        ost << s << '\n';
    }
}

int main()
{
    string f1 = "file_1.txt";
    string f2 = "file_2.txt";
    string f3 = "new_file.txt";
    // clear f3 from previous test
    ofstream ost{f3, ios::trunc };
    ost.close();

    append_file(f1,f3);
    append_file(f2,f3);

    return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

bool append_file( std::ifstream srce_file, std::ofstream& dest_file )
{ return dest_file << srce_file.rdbuf() && srce_file && srce_file.get() && srce_file.eof() ; }

int main()
{
    const std::string input_file_1 = __FILE__ ; // "file_1.txt";
    const std::string input_file_2 = __FILE__ ; // "file_2.txt";
    const std::string output_file = "new_file.txt";

    {
        std::ofstream new_file(output_file) ;
        
        if( append_file( std::ifstream(input_file_1), new_file ) &&
            append_file( std::ifstream(input_file_2), new_file ) )
        { std::clog << "*** files were appended ***\n\n" ; }
    }
}
// ------------------------------------------------------------ 

http://coliru.stacked-crooked.com/a/70c75b29b9894f02
http://rextester.com/DOFZG15135
Thanks. I'm working through Stroustrup's PPP book. Your technique for joining files looks more advanced that what's been introduced so far.

I'd still like to understand what is going wrong with my code. Any idea?
I'd still like to understand what is going wrong with my code. Any idea?

1
2
3
4
5
6
while(ist)
{
    string s;
    getline( ist, s, '\n'); // if this fails (say, there are no more lines to get)
    ost << s << '\n'; // we still write whatever is in s (s would be empty and an empty line would be written)  
}


This would be fine (check for input failure after attempted input):
1
2
3
4
5
6
bool append_file( std::ifstream srce_file, std::ofstream& dest_file )
{
    std::string line ;
    while( std::getline( srce_file, line ) ) dest_file << line << '\n' ;
    return dest_file && srce_file.eof() ;
}


This would be better:
(non-lounge-kiddie like: we know that std::getline() is not the one and only true way of performing robust input):
1
2
3
4
5
6
bool append_file( std::ifstream srce_file, std::ofstream& dest_file )
{
    char c ;
    while( srce_file.get(c) ) dest_file << c ;
    return dest_file && srce_file.eof() ;
}
Makes sense, thanks!
Topic archived. No new replies allowed.