Link to home
Start Free TrialLog in
Avatar of faehntim
faehntim

asked on

file input: get() after seekg()

After a file is traversed with get() I use seekg() to return to the beginning of the file.  This works okay, but when I use get() after I reach the beginning of the file, get() just doesn't get it (so to speak).  I don't understand this.  Here is some sample code compiled with DJGPP (hope it shows correctly):

#include <iostream.h>
#include <fstream.h>

main()
{
  char nextChar;
  ifstream readIn("gallery.txt");

  if(!readIn)
  {
     cout << "Can't find the file \"gallery.txt\"" << endl;
     return 0;
  }

  while(readIn.get(nextChar))
  {
    cout << nextChar;
  }

  cout << endl << "Place before seekg(): " << readIn.tellg() << endl;
  readIn.seekg(0, ios::beg);
  cout << endl << "Place after seekg(): " << readIn.tellg() << endl;

  cout << "=====================================================" << endl
       << "=====================================================" << endl;

  while(readIn.get(nextChar))
  {
    cout << nextChar;
  }

  return 0;
}

How do I get input from the second readIn.get()?  Why is it not inputting?  Is there something I'm doing wrong?
ASKER CERTIFIED SOLUTION
Avatar of mehlert
mehlert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mlev
mlev

This happens because the end-of-file flag is still set for readIn.
Do readIn.clear() to clear it (before or after seekg()).