File I/Os: Given two text files, each of which contains a a sorted list of integ
ID: 3669981 • Letter: F
Question
File I/Os: Given two text files, each of which contains a a sorted list of integers (one integer per line) in non-decreasing order, write a C++ program to merge these two input files into a third file in which all the numbers remain their sorted order. Your program will include the main() function and another function that merges the two files. Specifically, the main() function will ask a user to type in the names of the two input files. It will then call the merging function to merge the two files. Finally, it informs the user the name of the merged file. Note that you are not allowed to first load all the numbers in the two files into an array or vector then apply a sorting algorithm to this array.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
// Function declaration.
void get_in_file(ifstream&, const char*);
void get_out_file(ofstream&, const char*);
void caculate_write(ifstream&, ifstream&, ofstream&);
// Constant declaration
const char FILE_ONE[15] = "input1.dat";
const char FILE_TWO[15] = "input2.dat";
const char FILE_OUT[15] = "output.dat";
int main()
{
// Make the objects exist.
ifstream file_one, file_two;
ofstream file_out;
// Open the files to be used.
get_in_file(file_one, FILE_ONE);
get_in_file(file_two, FILE_TWO);
get_out_file(file_out, FILE_OUT);
// Merge the numbers in the file and write.
caculate_write(file_one, file_two, file_out);
// Close the open files for safety.
file_one.close();
file_two.close();
file_out.close();
return 0;
}
// The back bone to do the math and write to the file.
void caculate_write(ifstream& file_one, ifstream& file_two, ofstream& file_out)
{
cout << "Calculation starting..." << endl; // Notify the console.
int number_one, number_two, temp;
file_two >> number_two;
// This would be so much easer with an array. But I guess the point of
// this assignment is to use a loop in a loop, loop inception.
while (true)
{
file_one >> number_one;
// When the first file is over finish the second if needed.
if (file_one.eof())
{
while (file_two >> number_two)
{
file_out << number_two << endl;
}
break; // Kill the loop.
}
else
{
// Decide on which number to print to file.
if (number_one < number_two)
{
file_out << number_one << endl;
}
else if (number_one > number_two)
{
file_out << number_two << endl;
}
else
{
file_out << number_one << endl;
file_out << number_two << endl;
file_two >> number_two; // Get the next number from file two.
}
}
}
cout << "Calculation finished..." << endl; // Notify the console.
}
// Get the output file
void get_out_file(ofstream& file, const char* file_name)
{
file.open(file_name);
if (file.fail())
{
cout << "WARRING: " << file_name << " can't be opened." << endl;
exit(1);
}
cout << "NOTICE: " << file_name << " opened successfully." << endl;
}
// Get the input file.
void get_in_file(ifstream& file, const char* file_name)
{
file.open(file_name);
if (file.fail())
{
cout << "WARRING: " << file_name << " can't be opened." << endl;
exit(1);
}
cout << "NOTICE: " << file_name << " opened successfully." << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.