problem with reading from files.

I have two files which basically look like this:

file1:

232 JOHN SMITH 3.4  34 H
345 ADAM SILVER 3.6  3  A


file2:

232 4.0 S 2 Y
345 -3.0 M  4 N


I'm trying to read the info by first defining a struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

struct Employee
{
	
	char ID[10];
	char last[30];
	char first[30];
	double wage;
	double hrs;
	char dept;
	double change;
	bool single;
	int depend;
	bool ins;
};


And then read the info to a vector of struct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<Employee> employ;
checkFileName();
ifstream in1(file1);
ifstream in2(file2);
char yn;
int n = 1;


while ( ! ( in1.eof() || in2.eof()) )
{	
	Employee temp;
		
	in1 >> temp.ID >> temp.last >> temp.first >> temp.wage >> temp.hrs >> temp.dept;
	in2 >> temp.ID >> temp.change >> yn >> temp.depend;
	temp.single = toupper(yn) == 'S';
	in2 >> yn;
	temp.ins = toupper(yn) == 'Y';
            employ.push_back(temp);
		
	
}
in1.close();
in2.close();


And the print the vector content:
1
2
3
4
5
6
7
8
9
10
11
12

for ( int n = 0; n < employ.size(); ++n)
	cout << setw(5) << employ[n].ID
		<< setw(10) << employ[n].last
		<< setw(10) << employ[n].first
		<< setw(4) << employ[n].wage
		<< setw(4) << employ[n].hrs
		<< setw(2) << employ[n].dept
		<< setw(5) << employ[n].change
		<< setw(2) << ( employ[n].single ? "S" : "M")
		<< setw(3) << employ[n].depend
		<< setw(2) << ( employ[n].ins ? "Y" : "N") << endl << endl;


I expected the output to be:

232 JOHN  SMITH 3.4  34  H   4.0  S  2  Y
345 ADAM SILVER 3.6   3  A  -3.0  M  4  N

which is logically correct!
But for some reason what I got was actually:

232 JOHN  SMITH 3.4  34  H   4.0  S  2  Y
345 ADAM SILVER 3.6   3  A  -3.0  M  4  N
                3.6   3  A  -3.0  M  4  N
(Notice the last line, plz!)
This seems kinda odd to me!
I have no idea what's really goin' on, but my guess is that my code didn't stop reading even when it encountered the EOF marker, and worse it might've even moved back the pointer and read the last line again~!:S But why did it skip "345", "ADAM", AND "SILVER" ?!

Anyways, can anyone help me w/ this?

Last edited on
After reading the last value on the second lines of your files, the eofbit still hasn't been set, so this results in your program going through the loop a third time (giving you the strange result that I don't know how to explain).

I'm no expert, but I think you could solve this with a quick call to 'peek' at the end of each iteration of the while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
	while ( ! ( in1.eof() || in2.eof()) )
	{	
		Employee temp;

		in1 >> temp.ID >> temp.last >> temp.first >> temp.wage >> temp.hrs >> temp.dept;
		in2 >> temp.ID >> temp.change >> yn >> temp.depend;
		temp.single = toupper(yn) == 'S';
		in2 >> yn;
		temp.ins = toupper(yn) == 'Y';
		employ.push_back(temp);
		in1.peek();
		in2.peek();
	}



That worked for me, but I think there is probably a much nicer solution. You're making the assumption that the file will not contain invalid data. Perhaps reading each line as a string and using a stringstream would be a better solution, I'm not sure...

Hope that helps.
Topic archived. No new replies allowed.