In: Computer Science
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf
#include
#include
#include
using namespace std;
int main()
{
ifstream inFile1;
ifstream inFile2;
ofstream outFile1;
int mClientNumber, fClientNumber;
string mClientName;
string fClientName;
bool atLeastOneFileNotAtEnd = true;
bool inFile1Written = false;
bool inFile2Written = false;
cout << "File merge processing starting." <<
endl;
inFile1.open("MaleClients.rtf");
inFile2.open("FemaleClients.rtf");
outFile1.open("MergedClients.rtf");
inFile1 >> mClientNumber;
inFile1 >> mClientName;
inFile2 >> fClientNumber;
inFile2 >> fClientName;
while (atLeastOneFileNotAtEnd == true)
{
if(inFile1.eof())
{
if(inFile2Written == false)
{
outFile1 << fClientNumber << " " <<
fClientName << endl;
inFile2Written = true;
}
}
else if(inFile2.eof())
{
if(inFile1Written == false)
{
outFile1 << mClientNumber << " " << mClientName
<< endl;
inFile1Written = true;
}
}
else if (mClientNumber < fClientNumber)
{
outFile1 << mClientNumber << " " << mClientName
<< endl;
inFile1Written = true;
}
else
{
outFile1 << fClientNumber << " " << fClientName
<< endl;
inFile2Written = true;
}
if ((!inFile1.eof()) && (inFile1Written == true))
{
inFile1 >> mClientNumber >>
mClientName;
inFile1Written = false;
}
if ((!inFile2.eof()) && (inFile2Written == true))
{
inFile2 >> fClientNumber >> fClientName;
inFile2Written = false;
}
if ((inFile1.eof()) && (inFile2.eof()))
{
atLeastOneFileNotAtEnd = false;
}
}
inFile1.close();
inFile2.close();
outFile1.close();
cout << "Merging Complete." << endl;
return 0;
}
Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate
the question. Thank You So Much.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream inFile1;
ifstream inFile2;
ofstream outFile1;
int mClientNumber, fClientNumber;
string mClientName;
string fClientName;
bool atLeastOneFileNotAtEnd = true;
bool inFile1Written = false;
bool inFile2Written = false;
cout << "File merge processing starting."
<< endl;
inFile1.open("MaleClients.rtf");
inFile2.open("FemaleClients.rtf");
outFile1.open("MergedClients.rtf");
if (!inFile1.is_open() || !inFile2.is_open()){
cout<<"Errro!!! File not
found!!!";
}
inFile1 >> mClientNumber;
inFile1 >> mClientName;
inFile2 >> fClientNumber;
inFile2 >> fClientName;
while (atLeastOneFileNotAtEnd)
{
if(inFile1.eof())
{
if(inFile2Written == false)
{
outFile1 << fClientNumber << " "
<< fClientName << endl;
inFile2Written = true;
}
}
else if(inFile2.eof())
{
if(inFile1Written == false)
{
outFile1 << mClientNumber << " "
<< mClientName << endl;
inFile1Written = true;
}
}
else if (mClientNumber <
fClientNumber)
{
outFile1
<< mClientNumber << " " << mClientName <<
endl;
inFile1Written =
true;
}
else
{
outFile1
<< fClientNumber << " " << fClientName <<
endl;
inFile2Written =
true;
}
if ((!inFile1.eof()) &&
(inFile1Written == true))
{
inFile1 >>
mClientNumber >> mClientName;
inFile1Written =
false;
}
if ((!inFile2.eof()) &&
(inFile2Written == true))
{
inFile2 >>
fClientNumber >> fClientName;
inFile2Written =
false;
}
if ((inFile1.eof()) &&
(inFile2.eof()))
{
atLeastOneFileNotAtEnd = false;
}
}
inFile1.close();
inFile2.close();
outFile1.close();
cout << "Merging Complete." << endl;
return 0;
}
make sure the files are present and should be available for open.
MaleClients.rtf
1
2
3
4
5
4
5
6
7
8
9
FemaleClients.rtf
10 20
12 23
1 2
3 4
44 44
88 88
90 10
output