any help would be greatly appreciated

I need to write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function with three arguments: one for each of the two input file streams and one for the output file stream. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt.

I am not sure really where to start with this program and I am really struggling...any help would be great.
1
2
3
4
5
6
7

int main()
{

return 0;
};


Your program should define a function with three arguments: one for each of the two input file streams and one for the output file stream.


Since i need to output and input data in a function i need fstream and a function to do so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <fstream>
using namespace std;

void InputOuput(ifstream& Infile1, ifstream& Infile2, ofstream& Outfile)
{

  //do IO stuff here

};

int main()
{

//create the fstream objects  you will be using here
//call the InputOutput function

return 0;
};


Does this make sense?
okay this is what I have written myself....does it mean the same thing?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <fstream>
using namespace std;
int main()
{
    using namespace std;
    ifstream in_stream1, in_stream2;
    ofstream out_stream;
    
    in_stream1.open("numbers1.txt");
    in_stream2.open("numbers2.txt");
    out_stream.open("output.txt");
    
   
    return 0;
}
Without testing. It is possible that the code contains some typo.

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
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>

std::ostream & MergeFiles( std::istream &in1, std::istream &in2, std::ostream &out )
{
   std::merge( std::istream_iterator<int>( in1 ),
               std::istream_iterator<int>(),
               std::istream_iterator<int>( in2 ),
               std::istream_iterator<int>(),
               std::ostream_iterator<int>( out ) );  

   return ( out );
}


int main()
{
   const char * filename[] = { "numbers1.txt", "numbers2.txt", "output.txt" };

   std::ifstream in1( filename[0] );
   std::ifstream in2( filename[1] );
   std::ofstream out( filename[2] );

   if ( in1 && in2 && out ) MergeFiles( in1, in2, out );

   return 0;
}


EDIT: Oh, I wrote the code incorrectly. I thought about std::copy. Now the code is updated.
Last edited on
im sorry I ment to remove the second "using namespace std;" after int main()
closed account (3qX21hU5)
Here is a basic framework to help you get started. It won't compile but that is the point. It is just made so you can have something to get started on because like you said at times it can be very hard to figure out where to start.

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
#include <iostream>
#include <fstream>

using namespace std;

// Prototype of your function
void mergeFunction(const ifstream fileOne, const ifstream fileTwo,
                   ofstream outputFile);

int main()
{
    // The objects you will pass to your function
    ifstream fileOne("numbers1.txt"); // Holds file one
    ifstream fileTwo("numbers2.txt"); // Holds file two
    ofstream output("output.txt");
    
    // Might want to include error handling here for when there is a bad file,
    // or if the file fails to open, ect.
    
    // Main part of your program
    while (something)
    {
        /* This is where your function will go.
         * This is only a very basic framework so 
         * some of this you will want to change but it gives you a general
         * idea of how to start which can be challenging at times.
        */
        
    }
    
    return 0;
}

// Definition of your function
void mergeFunction(const ifstream fileOne, const ifstream fileTwo,
                   ofstream outputFile)
{
        // What your function does goes here. I believe you need to make it 
        // read from each file and output to the output file               
}


Otherwise another solution would be to break the whole program down into little projects. Like first start by making the function, then move on to handling the files, then move onto the main() part, ect.

Hope this helps a bit, and good luck.
I am a mere beginner at C++ programing I do not understand portions of your code….I am sorry, but thank you for your help…

for example I have never learned this portion of code bellow and hence I can not use it in my program...
1
2
3
4
5
std::merge( std::istream_iterator<int>( in1 ),
               std::istream_iterator<int>(),
               std::merge( std::istream_iterator<int>( in2 ),
                                  std::istream_iterator<int>(),
                                  std::ostream_iterator<int>( out ) ) );
but on a side note I am using Microsoft visual studio for the program
thank you Zereo (905) I will continue to work on it and see where it goes :)
std::merge is a standard algorithm that merges two ordered sequences. If you can not use std::merge you should write your own algorithm for merging ordered sequences.:)
closed account (3qX21hU5)
Vlad I am not it would be best to use the merge() function for this one. Mainly because the way I understood his assignment was he had to create a function to do it. And while your example does create one, I don't think that is what the professor meant. Though I guess it could go either way, but I would suggest the OP to be very knowledgeable about what merge() does if he is going to use it.

EDIT: Ahh your beat me to it
Last edited on
Here is a test program of the function

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	"stdafx.h"
#include	<iostream>
#include	<algorithm>
#include	<iterator>
#include	<sstream>
#include	<string>


int _tmain(int argc, _TCHAR* argv[])
{
	struct A
	{
		std::ostream & operator ()( std::istream &in1, std::istream &in2, std::ostream &out )
		{
			std::merge( std::istream_iterator<int>( in1 ),
			            std::istream_iterator<int>(),
			            std::istream_iterator<int>( in2 ),
			            std::istream_iterator<int>(),
			            std::ostream_iterator<int>( out ) );  

			return ( out );
		}
	};

	std::istringstream in1( " 1 3 5 7 9" );
	std::istringstream in2( " 0 2 4 6 8" );
	std::ostringstream out;

	std::cout << dynamic_cast<std::ostringstream &>( A()( in1, in2, out ) ).str() << std::endl;
	std::cout << std::endl;

	return 0;
}	



Output is

0123456789
Last edited on
Topic archived. No new replies allowed.