Writing to a single file from two files.

I created a simple program that writes some text to two different text files. Could someone show me how to create a program that would retrieve the information from those two files, and put them in a single file, first the content of input1 and then the content of input2. I am really needing help learning how to do this.
Here is the program that I created that creates the two files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	using namespace std;
	string input1;
	
	ofstream fout("input1.txt");
	fout<<"This is the first line in input1.txt.\n";
	cout<<"The first line was added to input1"<<endl;
	fout<<"This is the second line in input1.txt.\n";
	cout<<"The second line was added to input1"<<endl;
	fout<<"This is the third line in input1.txt.\n";
	cout<<"The third line was added to input1"<<endl;
	fout<<"This is the fourth line in input1.txt.\n";
	cout<<"The fourth line was added to input1"<<endl;
fout.close();

fout.open("input2.txt");
fout<<"This is the first line in input2.txt.\n";
cout<<"The first line was added to input2"<<endl;
fout<<"This is the second line in input2.txt."<<endl;
cout<<"The second line was added to input2"<<endl;
fout<<"This is the third line in input2.txt."<<endl;
cout<<"The third line was added to input2"<<endl;
fout.close();	

cin.get();
cin.get();
	return 0;
}
Could you consider reading http://www.cplusplus.com/doc/tutorial/files/ ?
Last edited on
I read it, but I don't understand it. I am needing a program that can open or read the content of input1 and create a single file, and then to do the same with input2, but have them both going to the same single file, instead of two different files.
If you did not understand a lot from the tutorial I am not quite sure you are going to understand this when I write it either. Let us try it for now though. I wrote a more general case, this will make you able to read contents of N files and add them to a single file in order. Suppose you have some preliminaries:
1
2
3
4
5
6
7
8
9
10
static const int FILES = 3;

static const char * fileName[ FILES ] = 
{
	"file1",
	"file2",
	"file3"
};

static const char * fileNameOut = "file4";


Reading the files could be something similar to following.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
ifstream is[ FILES ];
int length[ FILES ];
for( int f = 0; f < FILES; f++ )
{
	is[ f ].open( fileName[ f ], ios::binary );
	if( is[ f ].is_open( ) )
	{
		is[ f ].seekg( 0, ios::end );
		length[ f ] = is[ f ].tellg( );
		is[ f ].seekg( 0, ios::beg );
	}
	else
	{
		cout << "Unable to open file " << fileName[ f ] << ".\n";
		//Handle error
	}
}

int addedLength[ FILES ];
addedLength[ 0 ] = 0;
for( int f = 1; f < FILES; f++ )
	addedLength[ f ] = addedLength[ f - 1 ] + length[ f - 1 ];

char * b = new char[ addedLength[ FILES - 1 ] + length[ FILES - 1 ] ];

for( int f = 0; f < FILES; f++ )
{
	is[ f ].read( ( b + addedLength[ f ] ), length[ f ] );
	is[ f ].close( );
}


Continuing with same writing could be done as follows.
1
2
3
4
5
6
7
8
ofstream os( fileNameOut, ios::binary );
if( os.is_open( ) )
{
	os.write( b, addedLength[ FILES - 1 ] + length[ FILES - 1 ] );
	os.close( );
}
else
	cout << "Unable to open file " << fileNameOut << ".\n";


Notice I am using the std namespace.
Had you any questions, needed explanation or assist in understanding the code, feel free to ask.
Last edited on
Topic archived. No new replies allowed.