Reputation: 23
I am counting the number of non-whitespace characters in the standard input stream (cin) using in.get(). For every character which is not a whitespace character, I increment a counter.
After I'm done counting, I'd like to go back and read the characters. in is a parameter of type std::istream& in (cin in this case).
Here's what I am trying:
std::streampos sp = in.tellg();
while(in)
{
char c = in.peek();
if(isspace(c))
break;
++str.mSize;
in.get();
}
in.seekg(sp);
The value of sp is -1 indicating failure. Why?
Upvotes: 2
Views: 748
Reputation: 400374
Not all input streams are seekable—if stdin
is coming from a terminal or a pipe, it's not possible to seek it, forwards or backwards. In that case, you have to buffer the data yourself.
Upvotes: 5