[comp.lang.c++] Problems with putback

russb@alexa.JPL.NASA.GOV (Russ Brill) (05/21/91)

I know there has been some discussion of istream::putback() over the
last month, but I didn't care much until now.  We are getting failures
of this method (on Sun's cfront 2.0) at what appear to be rare and
random times.

Can you putback more than one character between extractions?  Must the
argument match the extracted character(s)?  Why might it otherwise
fail?

steve@taumet.com (Stephen Clamage) (05/21/91)

russb@alexa.JPL.NASA.GOV (Russ Brill) writes:

>I know there has been some discussion of istream::putback() over the
>last month, but I didn't care much until now.  We are getting failures
>of this method (on Sun's cfront 2.0) at what appear to be rare and
>random times.

>Can you putback more than one character between extractions?  Must the
>argument match the extracted character(s)?  Why might it otherwise
>fail?

You might not get more than one char of putback under certain circumstances
depending on the details of the implementation.  Putback bascically
moves back the pointer in the input buffer.  If the buffer were just
filled, you might be limited to one character at that time.  This would
account for rare and random failures.  Additionally, if the stream error
state is not zero, putback will do nothing.  If the input were unbuffered,
you might be limited to one character of putback.

The documentation for putback says that if the putback character does not
match the one extracted, the effect is undefined.  The reason for that
is you might be reading a strstream from read-only storeage.  When
putback moves back the input pointer, it would not be possible to change
what was pointed to.  Under ordinary circumstances, you can putback
anything, but you never know how much is safe to putback.

Iostreams implement a general-purpose text I/O system, but not an all-
purpose system.  Some things cannot conveniently be done with the
standard streams.  You can extend the streams package to do other
things, however.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

kpv@ulysses.att.com (Phong Vo[drew]) (05/22/91)

In article <739@taumet.com>, steve@taumet.com (Stephen Clamage) writes:
> 
> The documentation for putback says that if the putback character does not
> match the one extracted, the effect is undefined.  The reason for that
> is you might be reading a strstream from read-only storeage.
> ...
This is an example of a bad interface design (sorry jss).
The clause "the effect is undefined" is always a cause for problems
because programmers cannot rely on the interface. If putback() is designed
only to back up one input byte, then it should not require an input argument.
If it is to be as ungetc() in stdio, then it should simply say (as stdio does)
that only one byte can be pushed back regardless of whether or not it matches
the last input byte. I don't know what the equivalence of fseek()/ftell() is
for iostream but an interesting question is where would the logical stream
pointer be after putback().  If the implementation is not careful,
the effect is going to be not-simple-to-define.