Redirect cin to a string

c++, iostream

Solution

Like this:

#include <sstream>
#include <iostream>

std::istringstream stream("Some string 123");
streambuf* cin_backup = std::cin.rdbuf(stream.rdbuf());

You might want to back up the original rdbuf of std::cin, if you want to use it again.

Problem

I want to have `cin` read input from a string. Is there a way to do this? Something like this: ``` const char * s = "123 ab"; cin.readFrom(s); // <---- I want something like this int i; cin>>i; cout<<i; //123 ```

Original source