LucianNovo
LucianNovo

Reputation: 370

Reading input from std::cin TWICE

As I take input from cin using the cin.get function, it will automatically update the read location from the input file. What should I do to return the read location to the beginning of the file, so that I can take in the input a second time?

Say for instance I have the following file input.txt:

"Say hello to your new world"

and the following get loop to take in the input.txt file:

while(cin.get(charTemp)){
     numberOfChars++; 
} 

How could I take in the input twice?

Upvotes: 3

Views: 4061

Answers (2)

Dietmar Kühl
Dietmar Kühl

Reputation: 153840

You won't be able to reread the standard input stream. If you really need to read the content twice you'll have to store it, e.g.:

std::stringstream input;
input << std::cin.rdbuf();
input.seekg(0);
// use input and seek back to the beginning if needed

Upvotes: 7

David Xu
David Xu

Reputation: 5597

use rewind(stdin)

http://www.cplusplus.com/reference/cstdio/rewind/ for reference.

Upvotes: -1

Related Questions