I'm almost done with my assignment but now I'm stuck with some binary file problems. Below is the binary file section of my work.
cin.ignore();
cout << "\nPlease enter filename: ";
cin >> openBinary;
ifstream finBinary;
finBinary.open(openBinary, ios::in | ios::binary);
if (!finBinary.good())
cerr << "File could not be opened" << endl;
else
{
finBinary.read((char*)&students, sizeof(info)*MAX_STUDENTS);
finBinary.seekg(0, ios::end);
a = finBinary.tellg();
cout << a/sizeof(info) << endl;
for (int q = 0; q < MAX_STUDENTS; q++)
{
cout << "ID: " << students[q].ID << endl;
cout << "Name: " << students[q].name << endl;
cout << "Address: " << students[q].address << endl;
cout << "Telephone no.: " << students[q].telephone << endl
<< endl;
}
}
I can't get my program working when I put !finBinary.eof(). Google'ed and found out that binary files have some characters similiar to eof? So I decided to use the seekg and tellg, which gives me a weird number and not what I was expecting (which is 5 records in the binary file).
Anyone can tell me why?