c++ - Injecting string to 'cin' -


I have a function that reads user input from std :: cin, and I want to type in a letter that gives some strings Includes Std :: cin, such as std :: cin, afterwards, the extraction will read that string rather than pausing for keyboard input.

Ideally, I will change the function signature so that I have a custom easter parameter, but I can not do this because I have a fixed interface that I can not change.

cin.putback () About what I wanted, even though it is only one letter at one time, and it is on putting them in reverse order (but I read somewhere Is that the return could not be basically dangerous when returning, although the website is not detailed). I have tried several ways to inject the cin's inner buffer cin.rdbuf (), but no one will work either. I also considered using an external test script or subprosec, although I'd first consider a test in pure C ++.

So, is there any way to wire the wire? Or do you know a better way to inject your "fake keyboard input"?

If you really want to actually use Std :: cin, try it:

  int main () {namespace std; Streambuf * Backup; Istringstream oss ("test data"); Backup = cin.rdbuf (); Cin.rdbuf (oss.rdbuf ()); String str; Cin & gt; & Gt; Str; Cout & lt; & Lt; "Read" & lt; & Lt; Str; }  

When you are done with a backup, you can restore std :: cin to streambuf. I do not guarantee its portability, P


Comments