3

Is there a C++ version of ungetc?

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

0

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

istream::unget is not ungetc. You can lie with ungetc.
@Martinho: That's just putback instead
Probably also worth mentioning the peek method, because most wants for unget are just the second step of implementing "I want to peek the next character".
2

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

Comments

0

Yes, http://www.cplusplus.com/reference/iostream/istream/putback/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.