Help with this endless loop!

I need help. I'm still getting an endless loop here when reading from a binary file. The data is in the file ok. Any suggestions. I debugged, but it didn't help. This is just the read function. If you need to see the other parts of the program, please let me know.

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
void Read(fstream &file, PayInfo &employee)
{
     char answer;
     file.open("employee.dat", ios::in | ios::binary);
     if (!file)
     {
          cout << "Error opening file. Please try again.\n";
     }
     cout << "Here is the list of employees:\n\n";
     file.read((char *)&employee,sizeof(employee));
     //file.read(reinterpret_cast<char*>(&employee),sizeof(employee));
     
     while (!file.eof())
     {
           cout << "\nFull name: "; 
           cout << employee.nameId.fullname;
           cout << "\nEmployee ID#: ";
           cout << employee.nameId.id;
           cout << "\nHourly Rate: $";
           cout << employee.hourlyRate;
           file.read((char *)&employee,sizeof(employee));
           //file.read(reinterpret_cast<char*>(&employee),sizeof(employee));
     }
     cout << "\n\nThat's the last record in the file!\n";
     file.clear();
     file.close();
}
I would have assumed from the function signature that Read reads one employee object from an already open file stream and returns (although returning something to indicate success or failure in that case would be good.)

What does the definition of PayInfo look like?
Topic archived. No new replies allowed.