Solved

combine two text files.

Posted on 2004-03-25
6
173 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

759 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

20 Experts available now in Live!

Get 1:1 Help Now