combine two text files.

i want to insert text from one file to another existing text file(to the end of the second file),could someone help me? example is perfect!

Who is Participating?
jkrConnect With a Mentor Commented:
You'd use

ifstream is;
ofstream os; ( "infile.txt"); ( "appendfile.txt", ios_base::ous | ios_base::app); // open as "append"

while (!is.eof ()) { // read from input file

    string s;
    getline ( is, s);
    os << s.c_str (); // append to output file

os.close ();
is.close ();
itsmeandnobodyelseConnect With a Mentor Commented:
Some minor supplements to the sample of jkr:

1. Use  

#include <fstream>
using namespace std;

2. Change ios_base::ous --> ios_base::out  

3.    os << s.c_str () << endl; // append to output file and add a linefeed character

Regards, Alex

For a more C++ version, you can use iterator's and the copy algorithm:
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

void main(void)
  ifstream in("success.log"); // Open the input file
  in.unsetf(ios_base::skipws); // We want whitespace
  ofstream out("out.txt", ios_base::app | ios_base::out); // Open the output file
  copy(istream_iterator<char>(in), istream_iterator<char>(), ostream_iterator<char>(out)); // Copy the data from the ifstream to the ofstream
Do you want to append the contents of second file to the end of the first file?
Or do you want to insert the contents of the second file at some other location in the first file?

-- Dan
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.