.unget

can someone explain to me what is.unget() mean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Practice Coding Exercises
Using command line arguments read in the file called Horoscope.txt
*/

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

//A function which gets a word from the stream and returns the stream to the call
istream& getword(istream& is, string& word)
{
	char ch;
	while (is.get(ch))
	{
		if (isalpha(ch))
			break;
	}


	//'’'


	if (!is)
		return is;

	string buffer;
	buffer += ch;
	while (is.get(ch))
	{
		if (isalpha(ch))
			buffer += ch;
		else
			break;
	}

	if (is)
		is.unget();

	if (is.eof())
		is.clear(ios::eofbit);

	word = std::move(buffer);

	return is;
}

int main(int argc, char* argv[])
{
	//1. Read in the filename and open an input filestream
	ifstream inFile(argv[1]);
	//Test for success
	if (!inFile)
	{
		cerr << "Input file opening failed.\n";
		return EXIT_FAILURE;
	}

	//2. Get the words from the file and store in a vector
	vector<string> v;
	string word;
	while (getword(inFile, word))
		v.push_back(word);
	//Finished reading in file


	cout << "Finished reading in file!\n";


}
i found this example confusing , because it is using cin, what do i get if i combine is with unget ?

and what is difference btw unget and putback ?

another expression that i am not familiar with is word = std::move(buffer);
what do i get if i use unget with an infile stream object like is instead of using it with cin ?

also what does this line mean word = std::move(buffer); ?
aa
aa
aa
also what does this line mean word = std::move(buffer); ?
See:

http://en.cppreference.com/w/cpp/utility/move

It is actually like copy but might be a bit faster.

what is difference btw unget and putback ?
See:

http://www.cplusplus.com/reference/istream/istream/unget/?kw=unget
http://www.cplusplus.com/reference/istream/istream/putback/

unget() has no parameter. It restores the last char.
putback() has a parameter. It actually writes a char without moving the write index.

what do i get if i use unget with an infile stream object like is instead of using it with cin ?
There shouldn't be a difference. But I have never used it so just test it.
i understand that if u use cin , unget will put the character back into the stream, but i still don't understand if i combine unget with infile like infile.unget() . will it still put the last character back into the stream ?

i can comment out this section of code and i tried running without it and it has no effect:


if (is)
is.unget();
It has no effect because it is guaranteed that is (line 40) will return false and hence is.unget(); will not be called. But I would think it has no effect anyway since the stream is in an error state. What is the effect you want?
if unget puts the char back into the stream again for read, would not i read the same char twice
at the line while (is.get(ch)) ?
if unget puts the char back into the stream again for read, would not i read the same char twice
at the line while (is.get(ch)) ?
No, because it is not inside the loop. If it where inside you would read the same character over and over again which would lead to a infinite loop if the character where actually alpha.
Topic archived. No new replies allowed.