Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between putback() and unget()

Tags:

c++

I'm using a Standard iostream to get some input from a file, and I'm confused about unget() versus putback(character). It seems to me from the documentation that these functions are effectively identical, where unget() just remembers the character put in, so I'm nervous. I've always used putback(character), but character is always the last read character and I've been thinking about changing to unget(). Is putback(character) always identical to unget(), if character is always the last read character?

like image 509
Puppy Avatar asked Sep 05 '25 05:09

Puppy


1 Answers

You can't lie with unget(). It "ungets" the last-read character. You can lie with putback(c). You can "putback" some character other than the last-read character. Sometimes putting back a character other than the last-read character can be useful.

Also, if the underlying read buffer really does have buffering capability, you can "putback" more than one character. I think ungetc() is limited to one character.

Edit
Nope. It looks like unget() can go as far back as putback().

like image 94
David Hammen Avatar answered Sep 07 '25 18:09

David Hammen