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;

I'd be more interested in knowing why you need seekg() . This doesn't look like a binary search. Just looks like you're bouncing around a file for the sake of bouncing where just reading the file would be more efficient and less error-prone.

Yes, I know. Tell that to the professor who wanted the assignment to be that way. But anyway, thanks. I located the error and solved it.

This article has been dead for over six months. Start a new discussion instead.