Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads two input files whose lines are ordered by a key data

ID: 646292 • Letter: W

Question

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 same 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 File 2 Output file Adams C Barnes A Adams C Jones D Johnson C Barnes A King B Johnson C Jones D King B Using a text editor, create File 1 and File 2. 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 1 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. Include comments in your code: Description of problem Program input identified and defined Program output identified and defined Major algorithm steps commented

Explanation / Answer

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void mergeData();
int main()
{
mergeData();
return 0;
}
void mergeData(){
cout<<"Merging files started";
string firstfileName, File1_lName,secondfileName, File2_lName;
int firstfilenum, seconddfilenum;
ifstream inputfile1;
ifstream inputfile2;
ofstream finaloutputFile;
inputfile1.open("firstfile.txt", ios::in);
inputfile2.open("secondfile.txt", ios::in);
finaloutputFile.open("finaloutput.txt", ios::out);

inputfile1 >> firstfilenum >> firstfileName >> File1_lName;
inputfile2 >> seconddfilenum >> secondfileName >> File2_lName;
while (!inputfile1.eof() && !inputfile2.eof())
{
if (firstfilenum <= seconddfilenum)
{
finaloutputFile << firstfilenum << ' ' << firstfileName << ' '
<< File1_lName << " * ";
finaloutputFile << seconddfilenum << ' ' << secondfileName << ' '
<< File2_lName << " * ";
inputfile1 >> firstfilenum >> firstfileName >> File1_lName;
inputfile2 >> seconddfilenum >> secondfileName >> File2_lName;
cout << seconddfilenum << " " << secondfileName << " " << File2_lName << endl;
}
else if (inputfile1.eof())
{
while (!inputfile2.eof())
{
finaloutputFile << seconddfilenum << ' ' << secondfileName << ' '
<< File2_lName << ' ';
}
}
else if (inputfile2.eof())
{
while(!inputfile1.eof())
{
finaloutputFile << firstfilenum << ' ' << firstfileName << ' '
<< File1_lName << ' ';
}
}

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote