Won't read input file

Code executes, but does not pull info from input file?

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
  #include <fstream>
#include <iostream>
#include <string>

using namespace std;
int main()

{

ifstream inFile1;
ifstream inFile2;
ofstream outFile1;
int mClientNumber, fClientNumber;
string mClientName, 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 >> mClientName;
inFile2 >> fClientNumber >> fClientName;
	{

while ( atLeastOneFileNotAtEnd == true ) 
	
	if (inFile1.eof())
		{ 
		if (inFile2Written == false)
		outFile1 << fClientNumber << endl;
		outFile1 << fClientName << endl;
		inFile2Written = true;
		}
	
	else if (inFile2.eof())
	
	if (inFile1Written == false)
		{
		outFile1 << mClientNumber << endl;
		outFile1 << mClientName << endl;
		inFile1Written = true;
		}
	else if (mClientNumber < fClientNumber)
		{
		outFile1 << mClientNumber << endl;
		outFile1 << mClientName << endl;
		inFile1Written = true;
		}
	else 
		{	
		outFile1 << fClientNumber << endl;
		outFile1 << 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();
system("PAUSE");
return 0;
	}
}
Last edited on
Beside the bad formatting I don't know what your program should do solve, there is not any comment in.

What do you expect which will be be executed at the 'else' branch at line 37 ? At line 29? Are you forgetting the '!'s? What about the missing '{}' at line 31?

And I don't know how the if- and ofstream are implemented at Windows, but I guess they are only working rather on plain text then rtf-formatted files.
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
    ifstream finM("males.txt"), finF("females.txt");
    ofstream fout("out.txt");
    if (!finM || !finF || !fout) {
        cout << "Can't open one or more files\n";
        return 1;
    }

    string mName, fName;
    int mNum, fNum;
    finM >> mNum >> mName;
    finF >> fNum >> fName;

    while (finM || finF) {
        if (!finF || (finM && mNum < fNum)) {
            fout << mNum << '\n' << mName << '\n';
            finM >> mNum >> mName;
        }
        else {
            fout << fNum << '\n' << fName << '\n';
            finF >> fNum >> fName;
        }
    }
}


Or perhaps:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Person {
    int    num;
    string name;
public:
    bool operator<(const Person& p) { return num < p.num; }
    friend istream& operator>>(istream& is, Person& p) {
        return is >> p.num >> p.name;
    }
    friend ostream& operator<<(ostream& os, const Person& p) {
        return os << p.num << '\n' << p.name << '\n';
    }
};

int main() {
    ifstream finM("males.txt"), finF("females.txt");
    ofstream fout("out.txt");
    if (!finM || !finF || !fout) {
        cout << "Can't open one or more files\n";
        return 1;
    }

    Person m, f;
    finM >> m;
    finF >> f;

    while (finM || finF) {
        if (!finF || (finM && m < f)) {
            fout << m;
            finM >> m;
        }
        else {
            fout << f;
            finF >> f;
        }
    }
}

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
  
This is the pseudo code that was given. 
   Declarations
//         InputFile inFile1;
//         InputFile inFile2;
//         OutputFile outFile1;
//         num mClientNumber, fClientNumber
//         string mClientName, fClientName
//         bool atLeastOneFileNotAtEnd = true
//         bool inFile1Written = false
//         bool inFile2Written = false
//     output "File merge processing starting"
//     open inFile1 "MaleClients.rtf"
//     open inFile2 "FemaleClients.rtf"
//     open outFile1 "MergedClients.rtf"
//     read mClientNumber and mClientName from inFile1
//     read fClientNumber and fClientName from inFile2
//     while ( atLeastOneFileNotAtEnd == true )
//       if (inFile1 is EOF)
//         if (inFile2Written == false)
//           output fClientNumber, fClientName to Outputfile
//           inFile2Written = true
//         endif
//       else if (inFile2 is EOF)
//         if (inFile1Written == false)
//           output mClientNumber, mClientName to Outputfile
//           inFile1Written = true
//         endif
//       else if (mClientNumber < fClientNumber)
//         output mClientNumber, mClientName to Outputfile
//         inFile1Written = true
//       else
//         output fClientNumber, fClientName to Outputfile
//         inFile2Written = true
//       endif
//
//
//       if ((inFile1 not EOF) AND (inFile1Written == true))
//         read mClientNumber and mClientName from inFile1
//         inFile1Written = false
//       endif
//       if ((inFile2 not EOF) AND (inFile2Written == true))
//         read fClientNumber and fClientName from inFile2
//         inFile2Written = false
//       endif
//       if ((inFile is EOF) AND (inFile2 is EOF))
//         atLeastOneFileNotAtEnd = false
//       endif
//     endwhile
//     close inFile1
//     close inFile2
//     close outFile1
//     output "Merging Complete" 
Last edited on
I am obviously missing something here. This is suppose to read and merge the two files. It created an output file, and will open the command prompt and output message "File merging processing started" and just sits there. Also, nothing is written to the output file.
Last edited on
I simply can't help with that horribly written pseudocode. I guess I see how it works, but I can't fathom why anyone would write it that way. It's a travesty, man, a travesty.

But if you want to get your code working you'll need to fix up your spacing and put in all the braces.
Also please post a sample of your input files, and the expected output with those input files.

You should also invest in more meaningful variable names, and a few functions and a data structure or two wouldn't hurt either.
Last edited on
21 Barbara_Jones
82 Sue_Todd
275 Mary_Donald
276 Cathy_Conner
300 Debbie_Baker
400 Kim_Tanner


56 Bob_Johnson
100 Dan_Smith
200 Mike_Tay

The two sets of data are to be read, merged, and written to an output file.
Last edited on
Your filenames suggest that you use rtf files which contains lots of formatting codes.
I suggest that you save them as plain text files.

I offer this for some thought.

How do you know if the input files are actually open and that you have a good file stream.

Are the input files in the same directory as the output file?

Andy
Topic archived. No new replies allowed.