void function to merge two text files

Please see my code, it works fine by importing the merged two text files to a merged file. But am trying to use void function instead which is not working, I have removed my void function using //, any advise?? will also like to print it out and see how it looks on the console.
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  #include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include<fstream>
using namespace std;

//void merge (ifstream& myfile, ifstream& myfile2, ofstream& merged);

int _tmain(int argc, _TCHAR* argv[])
{

		
	ifstream myfile("C:\\chemicalelements1.txt", ios::in);
	ifstream myfile2("C:\\chemicalelements2.txt", ios::in);
	ofstream merged("C:\\merged.txt", ios::out);
	merged << myfile.rdbuf() << myfile2.rdbuf();


	myfile.close();
	myfile2.close();
	merged.close();
	
		
		

		
		
	system("PAUSE");
	return 0;
}

	/*void merge (ifstream& myfile, ifstream& myfile2, ofstream& merged) {
		
		
		ifstream myfile("C:\\chemicalelements1.txt", ios::in);
		ifstream myfile2("C:\\chemicalelements2.txt", ios::in);
		ofstream merged("C:\\merged.txt", ios::out);
		merged << myfile.rdbuf() << myfile2.rdbuf();

		
		myfile.close();
		myfile2.close();
		merged.close();
		 
		
		
	
}*/
You should probably not create any ifstreams inside the merge function.
After removing ifstreams inside the merge function, it seems to be working, but inside main, merged(); has an error underlined, not sure why but it still works. I also want to print it out on the console. Used: cout<<merged; does not work! any ideas?
Well merge() is a function that takes three parameters where have you called this function?

Last edited on
Do you understand what rdbuf() does? It returns a pointer to the stream's underlying buffer. http://www.cplusplus.com/reference/ios/ios/rdbuf/
You have no guarantee that all the data from the file is in the stream's buffer.

Your merge function won't compile. Lines 36-38 result in duplicate defined symbols (also declared as arguments on line 33).

Topic archived. No new replies allowed.