Please help! Thank you!! Write a program that reads two input files whose lines
ID: 3559725 • Letter: P
Question
Please help! Thank you!!
Write a program that reads two input files whose lines are ordered by a key data field. Your program should merge these two files, writing an output file that contains all lines from both files ordered by the key field. As an example, if two input files contain student names and grades for a particular class ordered by name, merge the information as shown below.
File 1:
Adams C
Jones D
King B
File 2:
Barnes A
Johnson C
Output file:
Adams C
Barnes A
Johnson C
Jones D
King B
You must read one line of a file at a time and either write it or the last line read from the other data file to the output file. A common merge algorithm is the following: Read a line from each data file While the end of both files has not been reached If the line from file 1 is smaller than the line from file 2 Write the line from file 2 to the output file and read a new line from file 1 Else Write the line from file 2 to the output file and read a new line from file 2. Write the remaining lines (if any) from file 1 to the output file. Write the remaining lines (if any) from file 2 to the output file.
Explanation / Answer
Complete Program to COPY:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void mergeTwoFiles(ifstream&, ifstream&, ofstream&);
int main()
{
string inFile1, inFile2, outFile;
ifstream in1;
ifstream in2;
ofstream out;
cout << "Enter input file 1: ";
cin >> inFile1;
cout << "Enter input file 2: ";
cin >> inFile2;
cout << "Enter output file: ";
cin >> outFile;
in1.open(inFile1);
if (in1.fail())
{
cerr << "Cannot open " << inFile1 << " for input." << endl;
system("pause");
return EXIT_FAILURE;
}
in2.open(inFile2);
if (in2.fail())
{
cerr << "Cannot open " << inFile2 << " for input." << endl;
system("pause");
return EXIT_FAILURE;
}
out.open(outFile);
if (out.fail())
{
cerr << "Cannot open " << outFile << " for output." << endl;
system("pause");
return EXIT_FAILURE;
}
mergeTwoFiles(in1, in2, out);
in1.close();
in2.close();
out.close();
system("pause");
return 0;
}
void mergeTwoFiles(ifstream &in1, ifstream &in2, ofstream &out)
{
string str1, str2;
getline(in1, str1);
getline(in2, str2);
while((!in1.eof()) && (!in2.eof()))
{
/* if the line from file 1 is smaller than the line from file 2 */
if(str1 <= str2)
{
/* write the line from file 1 to the output file */
out << str1 << endl;
// read a new line from file 1
getline(in1, str1);
}
/* if the line from file 2 is smaller than the line from file 1 */
else
{
/* write the line from file 2 to the output file */
out << str2 << endl;
// read a new line from file 2
getline(in2, str2);
}
}
/* write the remaining lines if any from file 1 to the output file */
while(!in1.eof())
{
out << str1 << endl;
getline(in1, str1);
}
if(str1 != "")
out << str1 << endl;
/* write the remaining lines if any from file 2 to the output file */
while(!in2.eof())
{
out << str2 << endl;
getline(in2, str2);
}
if(str2 != "")
out << str2 << endl;
}