Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

tellg() changes stream state?

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
PeteK
Asked:
PeteK
  • 2
1 Solution
 
abesoftCommented:
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
 
nietodCommented:
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
 
PeteKAuthor Commented:
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
 
nietodCommented:
Lucky guess.  :-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now