Hi guys. There seems to be a lot of people on this forum who know what they are talking about. My question is simple. I'm fairly new to c++ and i have hit a brick wall. I'm trying to write a simple program that grabs 2 txt files and outputs to a third. eg

Text1.txt
-----------
Hello everybody!


Text2.txt
----------
Great meeting you all


i want the output text to be this.

Output.txt
----------
Hello everybody!
great meeting you all

Simple thats all i need it to do, nothing else, no sorting nothing!! HELP!!!!

Recommended Answers

There are a couple ways to do it.

open file 1 for input

open result file for output

read each line in file 1 and write it to the result file. getline() will help you with that.

After done, close file 1 -- leave result file …

Jump to Post

>>If anyone has the actual code would they be able to paste it in?

Nope -- not happening here at DaniWeb. We don't do people's homework assignments for them.

Jump to Post

Well then do it in another language such as awk. Hell, if you are on MS-Windows you can even do it in a command prompt c:> copy file1+file2+file3 file4

Jump to Post

All 12 Replies

There are a couple ways to do it.

open file 1 for input

open result file for output

read each line in file 1 and write it to the result file. getline() will help you with that.

After done, close file 1 -- leave result file open

Now do the same thing with file 2.

thanks for replying. I know theres prob a few ways to do it. If anyone has the actual code would they be able to paste it in? Only if you already have it and it only takes a few secs to do it. Still scratching my head. back to the books for me i think! Damn..

>>If anyone has the actual code would they be able to paste it in?

Nope -- not happening here at DaniWeb. We don't do people's homework assignments for them.

It's not for homework. It's actually for a data entry job I have. If i can land this i can do a weeks worth in 20 seconds. It;s that important to me! Damn! Back to the drawing board again, or another forum i guess.

Well then do it in another language such as awk. Hell, if you are on MS-Windows you can even do it in a command prompt c:> copy file1+file2+file3 file4

really? SWEET! i'm going to give that a try. Cheers for this one. I'll try the command prompt idea.

Hi All. Question when I do a merge with the command prompt; say for example
c:> copy file1.txt+file2.txt file3.txt

file1.txt
--------
excellent

file2.txt
---------
cool


file3.txt (is turning out)
---------
excellentcool

file3.txt (would this be possible?)
---------------------
excellent
cool

any help guys?

or say i want.

1.txt
-----
wow cool
this is a test


2.txt
-----
can anyone help?
hello?

output.txt
--------
wow cool
this is a test
can anyone help?
hello?

ps- i program at a hello world level! :(

>>excellentcool

I'll bet text1 is not terminated with a '\n' (or Enter key) after the last line in the file

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    //declare two files for reading
    
    ifstream read1( "1.txt");
    ifstream read2("2.txt");
    
    //declare file for writing
    ofstream write ("3.txt");
    
    string line;
    string line2;
    
    while ( getline ( read1, line, '\n' ) )
    {
      write << line << endl;
    }
    
    while ( getline ( read2, line2, '\n' ) )
    {
      write << line2 << endl;
    }
    
    read1.close();
    read2.close();
    
    write.close();
    
}

Write a program that reads two files, file1.txt and file2.txt, and merges these two files to a third file named output.txt. The file file1.txt
contains even numbers between 0 and 10. The second file file2.txt contains odd numbers between 0 and 10. After you merge the files, the output file
contains all numbers from 0 to 10 in order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.