Please don\'t use a void function and this is a c++ question. Thanks Write a pro
ID: 3572971 • Letter: P
Question
Please don't use a void function and this is a c++ question. Thanks
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 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. You must write the merge algorithm yourself, do not use any code from the standard template library. Turn in: Analysis identifying program inputs, outputs, equations, and constants Design including human algorithm and pseudocode algorithm Test data Source codeExplanation / Answer
// headers used
#include <fstream>
#include <iostream>
using namespace std;
//main function befgins
int main () {
string line1,line2; //string varibles to store file lines temperarLy
// open a file in read mode.
ifstream infile1;
infile1.open("file1.txt");
ifstream infile2;
infile2.open("file2.txt");
// open a outfile in write mode.
ofstream outfile;
outfile.open("output.txt");
// while loop to read contents from two files and write data to out file
while (true)
{
bool x,y;
x=getline(infile1, line1); //is read data from file1
y=getline(infile2, line2); //is read data from file2
if(x&&y) //from both files data readed then condtjjion is true
{
//cout << line1 << endl;
//cout << line2 << endl;
if(line1.length()<line2.length()) //check length compariosn of two lines ,which line is max that is stored in outfile as first occurence
{
outfile << line2 << endl;
outfile << line1 << endl;
}
else
{
outfile << line1 << endl;
outfile << line2 << endl;
}
}
else if(x) //if only first file have data and second file reached eof then write file1 content to output file
{
outfile << line1 << endl;
}
else if(y) //if only second file have data and first file reached eof then write file2 content to output file
{
outfile << line2 << endl;
}
else //if both files reached to eof then close the loop
{
// close the opened file.
outfile.close();
break;
}
}
// open a output file in read mode to display output.
ifstream outf;
outf.open("output.txt");
string line3;
cout << "OUTPUT FILE DISPLAY" << endl;
while (getline(outf, line3))
{
cout << line3 << endl;
}
return 0;
}
INPUT FILE1 :
AAAA
BB
CCCCCC
DDDDDDDD
EEEEE
INPUT FILE2 :
FF
GGGGGGGGGGG
H
IIIIII
JJ
OUTPUT :
OUTPUT FILE DISPLAY
AAAA
FF
GGGGGGGGGGG
BB
CCCCCC
H
DDDDDDDD
IIIIII
EEEEE
JJ