Write a program that merges the integer numbers in two sorte

Write a program that merges the integer numbers in two sorted files and writes all the numbers in two a third file. Your program should prompt the user for the names of the two input files and the name of the output file. After the program is run, the output file will contain all the numbers in the two input files in one longer list in sorted order from smallest to largest. Your program should define a function that is called with two input file streams and the output file stream as three arguments. Sample Input/Output: Enter name for input fit-1: inputl.txt Enter name for input file-2: input2. Txt Enter name of output file: resuft.txt Your program should work for files of any length. Make sure to dose your files. Make sure to use meaningful variable names and proper line spacing and indentation. Include as comments in your program your name and a brief description of the program. Turn in a printout of your source code. Upload a copy of your source code to Sakai (Assignments: Program-4). Name your source file prog4.cpp

Solution

Please follow the code and comments for description :

CODE :

#include <iostream> // required header files
#include <string>
#include <fstream>

using namespace std;

int main() // driver method
{
   string inputinFile1, inputinFile12, outfile; // local variables
   cout << \"Enter the Name of the File 1 : \"; // prompt to enter the name
   cin >> inputinFile1; // get the file name
   cout << \"Enter the Name of the File 2 : \"; // prompt to enter the name
   cin >> inputinFile12; // get the file name
   cout << \"Enter the Name of the Output File : \"; // prompt to enter the name
   cin >> outfile; // get the file name
  
   ifstream inFile1(inputinFile1); // files for reading
   ifstream inFile2(inputinFile12);
  
   ofstream fout(outfile); // file for writing
   if (!inFile1.good() || !inFile2.good() || !fout.good()) // check if the file could be opened and edited or not
   {
       cout << \"Error opening file(s)\" << std::endl;
       return 0;
   }

   int data1; // required initialisations
   int data2;

   inFile1 >> data1; // read the data
   inFile2 >> data2;

   while (!inFile1.fail() && !inFile2.fail()) // check for the fail condition
   {
       if (data1 <= data2) // check for the data and compare it
       {
           fout << data1 << endl; // write the data to outfile
           inFile1 >> data1; // read the next data from the input
       } else {
           fout << data2 << endl; // write the data to outfile
           inFile2 >> data2; // read the next data from the input
       }
   }

   while (!inFile1.fail()) // check for the fail condition
   {
       fout << data1 << endl; // write the data
       inFile1 >> data1; // read again
   }

   while (!inFile2.fail()) // check for the fail condition
   {
       fout << data2 << endl; // write the data
       inFile2 >> data2;// read again
   }
   cout << \"Successfully Completed the Execution.!\" << endl;
   inFile1.close(); // close all the files respectively
   inFile2.close();
   fout.close();
}


OUTPUT :

a) input1.txt :

2
6
10
12
17


b) input2.txt :

4
8
9
15


c) out.txt :

2
4
6
8
9
10
12
15
17


Enter the Name of the File 1 : input1.txt
Enter the Name of the File 2 : input2.txt
Enter the Name of the Output File : out.txt
Successfully Completed the Execution.!


Hope this is helpful.

 Write a program that merges the integer numbers in two sorted files and writes all the numbers in two a third file. Your program should prompt the user for the
 Write a program that merges the integer numbers in two sorted files and writes all the numbers in two a third file. Your program should prompt the user for the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site