Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


tellg() changes stream state?

Posted on 2001-06-04
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 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