In C++, unget() makes an input stream take one step back. The character you read last becomes the next character again, so reading from the stream returns it a second time.
It is the simpler cousin of putback(). You do not name the character. The stream already knows which one to give back. Include <iostream> to use it.
This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.
Syntax
Call unget on an input stream right after reading a character. It takes no arguments.
C++
char c;
in.get(c);
in.unget(); // c will be read again
Call
What it does
in.unget()
Moves back one character.
in.get(c); in.unget();
Read a character and take it back at once.
if (!in.unget())
Tests whether stepping back worked.
unget() is good for one step back. Do not rely on going back several characters in a row.
Library declarations
C++
istream& unget();
Parameters
Parameter
Description
None
unget() takes no parameters.
Return Value
Return Value
Description
istream&
A reference to the stream. If stepping back is not possible, badbit is set and the stream is false in a condition.
C++ Examples
Example 1 — Read a Character and Take It Back
The same letter is read twice because unget() undoes the first read.
C++
#include <iostream>
#include <sstream>
int main() {
std::istringstream in("Hello");
char c = 0;
in.get(c);
std::cout << "read: " << c << '\n';
in.unget();
in.get(c);
std::cout << "read again: " << c << '\n';
return 0;
}
Output
read: H
read again: H
How It Works
The first get(c) reads H. The read position moves one step forward.
in.unget() moves the read position one step back, so H is the next character again.
The second get(c) reads H again.
Example 2 — Skip Letters, Then Read the Number
Read letters until a digit appears, step back, and read the whole number with >>.
C++
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream in("ab12");
std::string letters;
char c = 0;
while (in.get(c) && std::isalpha(c)) {
letters += c;
}
in.unget();
int n = 0;
in >> n;
std::cout << "letters: " << letters << '\n';
std::cout << "number: " << n << '\n';
return 0;
}
Output
letters: ab
number: 12
How It Works
The loop collects a and b. It stops when get(c) reads the 1, because 1 is not a letter.
The 1 has already been read, so unget() gives it back.
Now in >> n sees 12 from its first digit and reads the whole number.
Example 3 — unget() After the End of the Text
Read the only character, hit the end, and go back one step to read it again.
The first get() reads Z. The second one finds nothing and sets the stream flags, so the stream is not ok.
clear() resets those flags. The read position is still after Z.
unget() steps back one character, so the next get() returns Z again.
Frequently Asked Questions
It moves the read position of an input stream back by one character, so the last character read will be read again.
putback(c) takes the character as an argument. unget() takes no argument and gives back whichever character was read last.
Usually for one character, but it is not guaranteed. If it fails, the stream sets badbit, so check the result when it matters.
You should not count on it. Streams promise only one character of step back. Use a std::string for more look-ahead.
peek() looks at the next character without reading it, which is cleaner. Use unget() when you have already read the character and then decide you did not want it.