You are currently offline, waiting for your internet to reconnect

PRB: istream::seekg() Does Not Reset EOF State

This article was previously published under Q146445
This article has been archived. It is offered "as is" and will no longer be updated.
The istream::seekg() function does not reset the EOF (end of file) statewhen the stream pointer is already at EOF.
To work around this problem, call ios::clear() for the stream beforecalling istream::seekg().
This behavior is by design.
You don't need to call clear(), if the stream pointer is not alreadyat EOF.

Sample Code

/* Compile options needed: None*/ // Input: A file with the name test.txt containing some characters.#include <fstream.h>#include <assert.h>void main(){  fstream in("test.txt", ios::in);  char c;  int i = 0;  // Count characters in file  while (in.get(c))     ++i;  assert(in.eof());  // Reset read pointer to beginning of file  in.clear();  in.seekg(0, ios::beg);  // Check that file can now be read.  // Fails without the in.clear() statement above.  assert(!in.eof());}				

Article ID: 146445 - Last Review: 12/04/2015 14:09:53 - Revision: 1.1

Microsoft Visual C++ 4.0 Standard Edition, Microsoft Visual C++ 4.1 Subscription, Microsoft Visual C++ 4.2 Enterprise Edition, Microsoft Visual C++ 4.2 Professional Edition, Microsoft Visual C++ 5.0 Enterprise Edition, Microsoft Visual C++ 5.0 Professional Edition, Microsoft Visual C++ 6.0 Enterprise Edition, Microsoft Visual C++ 6.0 Professional Edition, Microsoft Visual C++, 32-bit Learning Edition 6.0

  • kbnosurvey kbarchive kbprb kbcpponly kbcompiler KB146445