Adding two text files into one text file



Dear Users and developers!

I have two or three text files! I want to add them to single text file!

say file1.txt, file2.txt, file3.txt

file
1
2
3
4

file2

5
6
7
8

file3.

10
20
30
30


combined file

1
2
3
4
5
6
7
8
10
20
30


Any help!!!!

With Best regards

Game
http://www.cplusplus.com/doc/tutorial/files/

just make sure you dont close the combined file, before you are done with all the output.
open dos prompt
type file1.txt file2.txt file3.txt > outputfile.txt

Either that or show us what you have so far
dear Samuel,

I didn't get your idea!

I am using c++, i have two text files (file1 and file2)

I want to add them together using c++ code!

That is my difficulty

Regards

1
2
3
4
5
std::ifstream file1( "file1.txt" ) ;
std::ifstream file2( "file2.txt" ) ;
std::ifstream file3( "file3.txt" ) ;
std::ofstream combined_file( "combined_file.txt" ) ;
combined_file << file1.rdbuf() << file2.rdbuf() << file3.rdbuf() ;

This is a method (There are many methods around) :
Create two string variables (buffer)
Use the function fread. Read both two files (means storing all results into two string variables (buffer)). Each file you should use a different buffer.

Use the function fwrite, write all results into a single file...

Other methods that I know : ReadFile - WriteFile ..... Ifstream - Ostream...
(Take a google search if necessary)
Hope this helps (see the example above)
Last edited on
Thank very much, this helps much!

Hi Game,

I know this is a C++ forum but you said nothing about programming in your post or what you needed to do, read a file, open a file, close a file, write.... People make such silly post on here, I was trying to be funny too :) I know it failed to be funny... :(

Normally we expect you to post your code of what you have tried, since there are a lot of students on here wanting someone to do their homework.

The simple way would be open a file
check to make sure it is open
open the output file
check to make sure it is open
read line
write line
continue until done
go to next file

I Like what JLBorges posted, I'll have to play withthat, I have not seen rdbuf() used before. If that works it looks much more condensed.



Dear Samuel,

Thank for your comment!

I am using a Libraries form Opencascade. I do not think necessary to post this coding here. One: It is to long to post from start, and second: Very different from c++ syntax. That is why!


Thank you very much for the answer,
some how busy with other staff now! I will post the possible answer soon!

Regards



Dear ALL,

Does anybody tried what JLBorges (1363) proposed: I tried as follow: But it combine the first two files, not both of them! Anybody tried?

1
2
3
4
5
6
7
8
9
10
11
12
                                        std::ifstream file1( "stern.stl");
					std::ifstream file2( "middle.stl");
					std::ifstream file3( "bow.stl");
					std::ifstream file4( "abdraft.stl");
					std::ifstream file5( "stern.stl.prop");
					std::ifstream file6( "middle.stl.prop");
					std::ifstream file7( "bow.stl.prop");
					std::ifstream file8( "abdraft.stl.prop");
                                        std::ofstream combined_file1( "/home/de052/Desktop/cd/original/Blohm Voss Naval/combined_file1.stl" ) ;
					combined_file1 << file1.rdbuf() << file2.rdbuf()<< file3.rdbuf() << file4.rdbuf();
					std::ofstream combined_file2( "/home/de052/Desktop/cd/original/Blohm Voss Naval/combined_file2.stl.prop" ) ;
				    combined_file2 << file5.rdbuf() << file6.rdbuf()<< file7.rdbuf() << file8.rdbuf();





In combinedfile, there is only files from file1 and file2, not file3 and file4.

What is wrong, is it for two files only?

Regards

Game
> In combinedfile, there is only files from file1 and file2, not file3 and file4.
> What is wrong, is it for two files only?

Make sure that file3 and file4 are in a good state.

Something like this perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool copy( const char* path_to_srce_file, std::ostream& dest_file  )
{
    std::ifstream srce_file( path_to_srce_file ) ;
    if( srce_file )
    {
        dest_file << srce_file.rdbuf() ;
        return dest_file ;
    }
    else
    {
        std::cerr << "error: could not open " << path_to_srce_file << '\n' ;
        return false ;
    }
}


And then

1
2
3
4
5
std::ofstream combined_file( "combined_file_path" ) ;
if( copy( "file1_path",  combined_file ) ) std::cout << "file1 appended\n" ;
if( copy( "file2_path",  combined_file ) ) std::cout << "file2 appended\n" ;
if( copy( "file3_path",  combined_file ) ) std::cout << "file3 appended\n" ;
// etc... 
Topic archived. No new replies allowed.