Solved

combine two text files.

Posted on 2004-03-25
6
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

726 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