Why does seekg() work only in the first iteration, while in the second iteration it doesn't.?However I used it in previous cases and it worked perfectly. And yes, I'm using clear(), but still nothing shows up after the first iteration (the iteration of the big while loop that encloses this part along with other parts). So, this case (case 3) works perfectly at the first iteration. In the second iteration while (binaryFile.eof()) returns false. So nothing in it's scope is executed.
Please Help!
Here is a snippet of the code:
case 3:
cout << endl << "Please Enter The Student's ID: ";
cin >> stID;
cout << endl;
binaryFile.open("file.txt",ios::binary|ios::in);
binaryFile.clear();
binaryFile.seekg(32);
counter=0;
while (!binaryFile.eof())
{
binaryFile.read (reinterpret_cast<char*>(&st.ID), sizeof(st.ID));
cout << "\ntellg: "<<binaryFile.tellg()<< endl;
cout << "st.level= " << st.ID <<"\nstLevel="<<stID ;
if (binaryFile.fail())
break;
if (st.ID == stID)
{
counter++;
cout << endl;
binaryFile.seekg( -36, ios::cur);
binaryFile.read (reinterpret_cast <char*> (&st), sizeof(st));
cout << st.firstName << setw(space2) << st.lastName;
cout <<setw(space2)<< st.ID ;
<< setw(space2) << st.level << setw(space2) << st.GPA;
cout <<setw(space2)<<st.grade << endl ;
binaryFile.seekg(32,ios::cur);
continue;
}
else
{
binaryFile.seekg(44,ios::cur);
}
}
if (counter == 0){
cout << endl << "I'm sorry, we don't have any student's with this ID." ;
cout << endl;
}
cout << "\ntellg: "<< binaryFile.tellg();
binaryFile.seekg(0, ios::beg);
binaryFile.clear();
binaryFile.close();
break;