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

Language C++ Givens: Key Program: OPmcopy key-fileMerge.run ./key-fileMerge.run

ID: 3877229 • Letter: L

Question

 Language C++  Givens:                   Key Program:    OPmcopy key-fileMerge.run                              ./key-fileMerge.run               Sample function: InFileValueCount.cpp   NOTE:        YOUR PROGRAM must behave EXACTLY LIKE key-fileMerge.run!!    Description: Design and write a program that merges the contents of two               sorted input files containing integer values into a single               sorted output file.  Required Input/Output Formats:       Input Prompt: Enter Name of Input File 1:                     Enter Name of Input File 2:                     Enter Name of Output File:                    Output labels/format: (x marks define field width)            INPUT FILE1 'infilename1' : CONTAINS xxx VALUES           INPUT FILE2 'infilename2' : CONTAINS xxx VALUES           INPUT FILE1 'infilename1' : SUM OF VALUES = xxxxxx            INPUT FILE2 'infilename2' : SUM OF VALUES = xxxxxx            OUTPUT FILE 'outfilename' : CONTAINS xxx VALUES           OUTPUT FILE 'outfilename' : SUM OF VALUES = xxxxxx                  ...   DESIGN CONSTRAINTS:      (1) Design and implement the following functions.        // ---------------------------------------------------------------       // Open specified input file.       // ---------------------------------------------------------------       void OpenInputFile(ifstream & inF, char inFileName[]);        // ---------------------------------------------------------------       // Return the number of values in input file (already open).       // NOTE: The function must first REWIND the file before starting.       // ---------------------------------------------------------------       int InFileValueCount (istream &inF);        // ---------------------------------------------------------------       // Read a value from an OPEN input file; set success to FALSE if       //      value can not be read.       // ---------------------------------------------------------------       void ReadValue (istream &inF, int & num, bool & success);        // ---------------------------------------------------------------       // Write a value to output stream, eight(8) values per line.       // Separate values by ONE SPACE.       // ---------------------------------------------------------------       void WriteValue (ostream & outF, int num);        // ---------------------------------------------------------------       // Return the sum of values in input file (already open).       // NOTE: The function must first REWIND the file before starting.       // ---------------------------------------------------------------       int Sum (istream &inF);     (2) DO NOT USE ARRAYS.    EXAMPLES:    Input file1:  1 2 3 6 10 13   Input file2:  1 4 5 6 10 12 14   Output file:  1 1 2 3 4 5 6 6                  10 10 12 13 14                     Input file1:  1 2 3   Input file2:     Output file:  1 2 3                     Input file1:     Input file2:  1 2 3     Output file:  1 2 3      Input file1:  5 7 8     Input file2:     Output file:  5 7 8      Input file1:  (empty)    Input file2:  (empty)      Output file:  (empty)        Input file1:  missing        OR       Input file1:  2 3    Input file2:  1 2 3                   Input file2:  missing    Output file:  not created.            Output file:  not created.     Message: FATAL ERROR: Unable to OPEN file 'file1'  (file2) 

Explanation / Answer

//This is a program for merging two files into third file
//First we will enter the first file name
//And then enter the second file name
//Finally we will merge them into third file

#include<stdio.h>
#include<stdlib.h>

#include<iostream.h>
#include<conio.h>

#include<fstream.h>

using namespace std;

main()
{

ifstream file1,file2;
ofstream ifilet;
char chh, f_name1[15], f_name2[15], f_name3[20];
cout<<"Enter first file: ";
gets(f_name1);
cout<<"Enter second file: ";
gets(f_name2);
cout<<"Enter Third file name: ";
gets(f_name3);
file1.open(f_name1);
ifiles2.open(f_name2);
if(file2==NULL || file1==NULL)
{
perror("error comes");
cout<<"press any alphabet to exit... ";
getch();
exit(EXIT_FAILURE);
}
ifilet.open(f_name3);
if(!ifilet)
{
perror("error");
cout<<"Press any key to exit... ";
getch();
exit(EXIT_FAILURE);
}
while(file1.eof()==0)
{
ifiles1>>chh;
ifilet<<chh;
}
while(file2.eof()==0)
{
file2>>chh;
ifilet<<chh;
}
cout<<"The file1 & file2 are merged into "<<f_name3<<" file ";
file1.close();
file2.close();
//file1 and file2 are closed
ifilet.close();
getch();
}