Solved

combine two text files.

Posted on 2004-03-25
6
175 Views
Last Modified: 2010-04-01
hi
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!
thanks.

0
Comment
Question by:shean85
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 10681105
You'd use

ifstream is;
ofstream os;

is.open ( "infile.txt");
os.open ( "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 ();
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 150 total points
ID: 10692378
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

0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10695954
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
}
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10711755
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now