tellg() changes stream state?

Posted on 2001-06-04
Last Modified: 2008-02-01
I'm having a problem loading data from a text file: some fields are read from the file missing their first character.

To debug this problem I have inserted a number of tellg() calls.  Unfortunately, the debugging process itself is behaving in unexpected ways.

To my dismay, the streampos returned by tellg() is affected by previous calls to tellg()!

For instance, this code snippet:    


  //Read in colour data for this object
  LoadStream >>m_Colour.x

  streampos TestPos = LoadStream.tellg();


returns a different value for TestPos than this


  // Additional call to tellg()
  streampos TestPos2 = LoadStream.tellg();

  //Read in colour data for this object
  LoadStream >>m_Colour.x

  streampos TestPos = LoadStream.tellg();


In the first case, TestPos == 526; In the second case, TestPos == 525.

This makes debugging difficult because my means of observation is affecting what is being observed!
Environment:  WinNT 4.0 SP6, Visual C++ 6.0


Is this normal behaviour (I noticed tellg() is not 'const')?  
If so, how do tellg() calls affect other tellg() calls?
If not, any ideas about what is going on and how to fix it(or how to figure out what is going on)?
Question by:PeteK
  • 2

Expert Comment

ID: 6153672
In the past, I've found the MS implementation of streams, er, flakey.  On reading files using ifstream, occassionally bytes were dropped from the input.  They were not necessarily at the start of a field, but they were reproducable.

I thought I was losing my mind....

Given the tight deadline that I was under, I created a class that read the entire file into memory, and constructed a istrstream to read the data from memory.  The problem went away.

(And unfortunately, I never did get back to figuring out what the real problem was.  I'd hate to say that it was an MS failure, since I tend to look only to my own code when searching for bugs.  I've never had much luck sending bug reports into MS anyhow.....)

If you want, I could post my work-around.
LVL 22

Accepted Solution

nietod earned 200 total points
ID: 6153727
The streams that ship with VC suffer from two problems that coudl be causing this.  First of all, they often attempt to read ahead one character beyond what you request and store this buffered character.  If this read ahead-was performed invissibly that would be fine, unforunatley, its not invissible, it produced detectable and incorrect changes to the behavior.  2nd they treat carriage return and line feeds as newline characters.  i.e. when reading a pair of characters  it must smetimes treat them as a single character.  This makes ithe conversion to and from stream possitions a little complex and it allows room for mistakes in the conversions and related code.

Are you reading the file sequentially or are you seeking back and forth in the file?  If you are seeking back and forth in the file, then you probably should have the file open as a binary file.  This will probalby help.   (It shouldn't be necessary, but it helps in VC.)

Author Comment

ID: 6153831
Thanks Abesoft and Nietod.  

Abesoft: Thanks for your comment.  It was good to hear of your frustration with MS streams.  I was greatly relieved by the suspicion that MS streams are flakey.

Nietod:  Thanks a lot.  Easiest fix ever!  I changed everything to binary and my original problem disappeared.

LVL 22

Expert Comment

ID: 6153875
Lucky guess.  :-)

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now