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?
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With