Click to See Complete Forum and Search --> : basic_istream::putback
Skip Sailors
June 24th, 1999, 08:59 AM
I read a file a character at a time using istream::get(), and every once in awhile I want to putback or unget. I am having trouble determining when putback is supposed to fail. I would love to RTFM, but am having trouble finding the section of the FM to FR. Help?
TIA
"An HWND is a terrible thing to waste."
Dave Lorde
June 24th, 1999, 11:07 AM
When in doubt, check the source. For istream (old I/O streams) it is:
istream& istream::putback(char c)
{
if (good())
{
lockbuf();
if (bp->sputbackc(c)==EOF)
{
clear(state | ios::failbit);
}
unlockbuf();
}
return *this;
}
for basic_istream (new standard streams) the fail conditions are: If rdbuf() is a null pointer, or if the call to sputbackc returns T::eof(), the function calls setstate(badbit). In any case, it returns *this.
Dave
Skip Sailors
June 25th, 1999, 03:30 PM
Thanks. So probably sputback returns and EOF for some reason.
Now I have another question. When is it expected that an sputback should fail. Yes, I read the code, and I started to see a few underscores, meaning I had wandered into the realm of implementation depenedencies.
I am not trying to see _how_ it is done as much as I'm trying to learn the _intent_. My specific situation is that I am reading a single character and I want to put it right back, and some times it doesn't go.
"An HWND is a terrible thing to waste."
Dave Lorde
June 28th, 1999, 05:06 AM
> When is it expected that an sputback should fail.
sputback will fail if the stream's 'get' pointer is above the lower bound of the get area (eback) and the character just before the 'get' pointer (gptr) is not the character passed as the argument. If the stream's 'get' pointer is not above the lower bound of the get area, it will call pbackfail.
pbackfail will return EOF if the previous character in the buffer is EOF. Otherwise, if the stream is buffered and there's a valid end of 'get' area pointer (egptr), it will insert the passed character into the buffer (moving subsequent characters up by one, and dropping the last one).
I don't know why your call is failing, but I'd suggest stepping through it with the debugger.
Dave
Skip Sailors
June 28th, 1999, 11:08 AM
Thank you for the help.
Another thing I am looking for, then, is trying to putback() a character that is _not_ the last one I retrieved. You can't put back characters that weren't there in the first place. Makes sense.
"An HWND is a terrible thing to waste."
Dave Lorde
June 29th, 1999, 04:15 AM
If you want to 'putback' a character that wasn't in the buffer in the first place, I think you may have to derive your own streambuf and override sputbackc (and, perhaps, pbackfail) so as not to reject unexpected characters. It shouldn't be too difficult, (although it would break the standard streambuf contract).
Another point is that the new character would only appear to be in the stream for the duration of a single buffer, and would evaporate if the buffer was refreshed.
Dave
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.