Merging Files Beginner

The program is not actually merging the Files. It just output "Master File Updating Starting" and stays there forever.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
	ifstream masterFile;
	ifstream transactionFile;
	ofstream newMasterFile;
	double mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
	string mClientfName, mClientlName;
	cout << "Master File Updating Starting" << endl;
	masterFile.open("Master.rxt");
	transactionFile.open("Transaction.rxt");
	newMasterFile.open("newMaster.rxt");
	masterFile >> mClientNumber;
	masterFile >> mClientfName;
	masterFile >> mClientlName;
	masterFile >> mtotalClientCost;
	transactionFile >> tClientNumber;
	transactionFile >> titemClientCost;
	while (!transactionFile.eof())
	{
		while ((!masterFile.eof()) && (mClientNumber < tClientNumber))
		{
			newMasterFile << mClientNumber;
			newMasterFile << mClientfName;
			newMasterFile << mClientlName;
			newMasterFile << mtotalClientCost;
			masterFile >> mClientNumber;
			masterFile >> mClientfName;
			masterFile >> mClientlName;
			masterFile >> mtotalClientCost;
		}
		if (masterFile.eof())
		{
			cout << "Error Client ID:  " << tClientNumber << "  not in Master File" << endl;
		}
		else if(mClientNumber == tClientNumber)
		{
			mtotalClientCost = mtotalClientCost + titemClientCost;
			newMasterFile << mClientNumber;
			newMasterFile << mClientfName;
			newMasterFile << mClientlName;
			newMasterFile << mtotalClientCost;
			masterFile >> mClientNumber;
			masterFile >> mClientfName;
			masterFile >> mClientlName;
			masterFile >> mtotalClientCost;
		}
		else if (mClientNumber > tClientNumber)
			cout << "Error Client ID:  " << tClientNumber << "  not in Master File" << endl;
		transactionFile >> tClientNumber;
		transactionFile >> titemClientCost;
	}
	
	while (!(masterFile.eof()))
	{
		newMasterFile << mClientNumber;
		newMasterFile << mClientfName;
		newMasterFile << mClientlName;
		newMasterFile << mtotalClientCost;
		masterFile >> mClientNumber;
		masterFile >> mClientfName;
		masterFile >> mClientlName;
		masterFile >> mtotalClientCost;
	}
	cout << "Master File Updating Complete" << endl;
	masterFile.close();
	transactionFile.close();
	newMasterFile.close();
	system ("PAUSE");
	return 0;
}
while (!(masterFile.eof())) is not ending i think its not able to read the EOF character
Generally, testing for eof() is not good practice - for several reasons, the first of which is that there could be some other (error) condition arising which prevents end of file from being reached.

You could change for example .eof() to .fail() throughout. In fact testing this condition is so common that there is a more concise way of doing so. Instead of writing while (!(masterFile.eof())) the syntax while (masterFile) gives the same effect.
See http://www.cplusplus.com/reference/ios/ios/operator_bool/

In this particular case, most probably one or both the input files was not opened successfully.
1
2
3
4
5
6
7
8
9
10
11
12
13
    ifstream masterFile("Master.rxt");              // declare and open the file
    if (!masterFile)                                // check that it was opened
    {
        cout << "Error opening master file\n";
        return 1;
    }
    
    ifstream transactionFile("Transaction.rxt");    // declare and open the file
    if (!transactionFile)                           // check that it was opened
    {
        cout << "Error opening transaction file\n";
        return 2;
    } 


read the first items from each file, then
1
2
3
4
    while (transactionFile)
    {
        while (masterFile && (mClientNumber < tClientNumber)) 
       //     etc. 






Are you sure your files are actually opening? If the file fails to open or some other failure happens eof() will never happen. And you really shouldn't be using eof() to control your read loops, this can lead to problems such as extra data appearing to be read. Instead use the actual read operation to control the read loops.

By the way there is no EOF character with modern operating systems, EOF is a condition not a character.

Also please post a small sample of your input file.

Last edited on
// Start
// Declarations
// InputFile masterFile;
// InputFile transactionFile;
// OutputFile newMasterFile;
// num mClientNumber, mtotalClientCost, tClientNumber, titemClientCost
// string mClientfName, mClientlName
// output "Master File Updating Starting"
// open masterFile "Master.rtf"
// open transactionFile "Transaction.rtf"
// open newMasterFile "newMaster.rtf"
// read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
// read tClientNumber, titemClientCost from transactionFile
// while ( transactionFile not EOF )
// while (( masterFile not EOF) and (mClientNumber < tClientNumber))
// output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
// read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
// endwhile
// if (masterFile is EOF)
// output "Error Client ID: ", tClientNumber, " not in Master File."
// else if (mClientNumber == tClientNumber) then
// mtotalClientCost = mtotalClientCost + titemClientCost
// output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
// read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
// else if (mClientNumber > tClientNumber) then
// output "Error Client ID: ", tClientNumber, " not in Master File."
// endif
// read tClientNumber, titemClientCost from transactionFile
// endwhile
// while (masterFile not EOF)
// output mClientNumber, mClientfName, mClientlName, mtotalClientCost to newMasterFile
// read mClientNumber, mClientfName, mClientlName, mtotalClientCost from masterFile
// endwhile
// output "Master File Updating Complete"
// close masterFile
// close transactionFile
// close newMasterFile
// Stop

Transaction.rtf
1 568.34
5 345.10
6 1012.43
17 2045.12
100 1231.00
101 167.39
125 5239.67

Master.rtf
5 Mike Smith 2098.72
6 Sue Nathan 1234.32
100 Bobby Jones 519.69
125 Sally Mayer 345.74
200 Danny Glover 5623.18
Chervil
I edit the code to the following as you told me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
	ifstream masterFile;
	ifstream transactionFile;
	ofstream newMasterFile;
	double mClientNumber, mtotalClientCost, tClientNumber, titemClientCost;
	string mClientfName, mClientlName;
	cout << "Master File Updating Starting" << endl;
	masterFile.open("Master.rtf");
	if (!masterFile)                                // check that it was opened
    {
        cout << "Error opening master file\n";
        return 1;
    }
    
	transactionFile.open("Transaction.rtf");
	if (!transactionFile)                           // check that it was opened
    {
        cout << "Error opening transaction file\n";
        return 2;
    } 
	newMasterFile.open("newMaster.rtf");
	masterFile >> mClientNumber;
	masterFile >> mClientfName;
	masterFile >> mClientlName;
	masterFile >> mtotalClientCost;
	transactionFile >> tClientNumber;
	transactionFile >> titemClientCost;
	while (!transactionFile.eof())
	{
		while ((!masterFile.eof()) && (mClientNumber < tClientNumber))
		{
			newMasterFile << mClientNumber;
			newMasterFile << mClientfName;
			newMasterFile << mClientlName;
			newMasterFile << mtotalClientCost;
			masterFile >> mClientNumber;
			masterFile >> mClientfName;
			masterFile >> mClientlName;
			masterFile >> mtotalClientCost;
		}
		if (masterFile.eof())
		{
			cout << "Error Client ID:  " << tClientNumber << "  not in Master File" << endl;
		}
		else if(mClientNumber == tClientNumber)
		{
			mtotalClientCost = mtotalClientCost + titemClientCost;
			newMasterFile << mClientNumber;
			newMasterFile << mClientfName;
			newMasterFile << mClientlName;
			newMasterFile << mtotalClientCost;
			masterFile >> mClientNumber;
			masterFile >> mClientfName;
			masterFile >> mClientlName;
			masterFile >> mtotalClientCost;
		}
		else if (mClientNumber > tClientNumber)
			cout << "Error Client ID:  " << tClientNumber << "  not in Master File" << endl;
		transactionFile >> tClientNumber;
		transactionFile >> titemClientCost;
	}
	
	while (!(masterFile.eof()))
	{
		newMasterFile << mClientNumber;
		newMasterFile << mClientfName;
		newMasterFile << mClientlName;
		newMasterFile << mtotalClientCost;
		masterFile >> mClientNumber;
		masterFile >> mClientfName;
		masterFile >> mClientlName;
		masterFile >> mtotalClientCost;
	}
	cout << "Master File Updating Complete" << endl;
	masterFile.close();
	transactionFile.close();
	newMasterFile.close();
	system ("PAUSE");
	return 0;
}

It was actually not opening the files since in the previous code I had .rxt when the files were saved as .rtf Yes... beginner mistake.
now the program runs perfectly
Thank You so much for all your help!!! :)
now the program runs perfectly

.. or appears to. Just a reminder, the use of eof() can be fragile and give rise to several different problems, such as processing the last line or entry in the file twice. It is not recommended.
ok. Thank You I will write that on my notes. :)
Topic archived. No new replies allowed.