You do not need nor may you use arrays for this program. Write a program that me
ID: 3573937 • Letter: Y
Question
You do not need nor may you use arrays for this program. Write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order, that is, they start small and get bigger as they go. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function to merge the file with three arguments: one for each of the two input FILE pointers and one for the output FILE pointer. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt. Merging the files will work by trying to read one number from each file. If you have two numbers, compare them and put the smaller one in the output file. Then try to get a new number from the file that you read from to output the last number. If at any point you only have a number from one file, because the other file is empty, then you can simply read and output the numbers one at a time until that file is empty.
Explanation / Answer
Solution:
/*Write a program that merges the numbers in two files and writes the results to a third file. The program reads input from two different files and writes the output to a third file. Each input file contains a list of integers in ascending order, that is, they start small and get bigger as they go. After the program is executed, the output file contains the numbers from the two input files in one longer list, also in ascending order. Your program should define a function to merge the file with three arguments: one for each of the two input FILE pointers and one for the output FILE pointer. The input files should be named numbers1.txt and numbers2.txt, and the output file should be named output.txt. Merging the files will work by trying to read one number from each file. If you have two numbers, compare them and put the smaller one in the output file. Then try to get a new number from the file that you read from to output the last number. If at any point you only have a number from one file, because the other file is empty, then you can simply read and output the numbers one at a time until that file is empty. **/
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void mergeFiles( ifstream& fin1, ifstream& fin2, ofstream& fout );
void showFile( const char* fname );
int main()
{
ifstream fin1( "input1.txt" );
ifstream fin2( "input2.txt" );
ofstream fout( "output.txt" );
mergeFiles( fin1, fin2, fout );
fout.close();
showFile( "input1.txt" );
showFile( "input2.txt" );
showFile( "output.txt" );
cout << " Press 'Enter' to continue/exit ... " << flush;
cin.get();
return 0;
}
void mergeFiles( ifstream& fin1, ifstream& fin2, ofstream& fout )
{
int i1, i2, countIn = 0, countOut = 0;
if( fin1 >> i1 ) ++ countIn;
if( fin1 ) // i.e. count == 1
{
if( fin2 >> i2 )
{
++ countIn;
while( fin1 && fin2 )
{
if( i1 <= i2 )
{
fout << i1 << ' ';
++countOut;
if( !(fin1 >> i1) )
{
fout << i2 << ' ';
++countOut;
//break;
}
else ++countIn;
}
else
{
fout << i2 << ' ';
++countOut;
if( !(fin2 >> i2) )
{
fout << i1 << ' ';
++countOut;
//break;
}
else ++countIn;
}
}
}
else
{
fout << i1 << ' ';
++countOut;
}
}
while( fin1 >> i1 ) { fout << i1 << ' '; ++countIn; ++countOut; }
while( fin2 >> i2 ) { fout << i2 << ' '; ++countIn; ++countOut; }
cout << "countIn = " << countIn << endl;
cout << "countOut = " << countOut << endl;
}
void showFile( const char* fname )
{
ifstream fin( fname );
if( fin )
{
cout << " Showing numbers in file "
<< fname << " ... ";
int tmp;
while( fin >> tmp ) cout << setw( 7 )<< tmp << ' ';
fin.close();
cout << endl;
}
else cout << " There was a problem opening file "
<< fname << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.