Solved

tellg() changes stream state?

Posted on 2001-06-04
4
300 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
[X]
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
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

622 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