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.
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>
usingnamespace 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;
};
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.
#include <iostream>
#include <fstream>
usingnamespace 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.
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.:)
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.