Solved

tellg() changes stream state?

Posted on 2001-06-04
4
286 Views
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
  >>m_Colour.y
  >>m_Colour.z
  >>m_Colour.w;

  streampos TestPos = LoadStream.tellg();

//////////////////////

returns a different value for TestPos than this
code:

//////////////////////

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

  //Read in colour data for this object
  LoadStream >>m_Colour.x
  >>m_Colour.y
  >>m_Colour.z
  >>m_Colour.w;

  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

Questions:

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)?
0
Comment
Question by:PeteK
  • 2
4 Comments
 
LVL 2

Expert Comment

by:abesoft
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.
Gene
0
 
LVL 22

Accepted Solution

by:
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.)
0
 

Author Comment

by:PeteK
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.

Relief!
0
 
LVL 22

Expert Comment

by:nietod
ID: 6153875
Lucky guess.  :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

766 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