fredoverflow
fredoverflow

Reputation: 263350

Is there a C++ version of ungetc?

Is there a C++ version of ungetc?

That is, can I put a character back onto an istream?

Upvotes: 3

Views: 1731

Answers (3)

user195488
user195488

Reputation:

Yes. istream::putback is the same as ungetc except if using unget():

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.

Upvotes: 6

R. Martinho Fernandes
R. Martinho Fernandes

Reputation: 234654

You can put a character back with the istream::putback function. You can even put a different character if you want.

Upvotes: 2

Related Questions