Redirect cin to a string

前端 未结 4 688
长发绾君心
长发绾君心 2020-12-16 19:14

I want to have cin read input from a string.

Is there a way to have it do this?

Something like this:

const char * s = \"123 ab\";
cin.readFro         


        
4条回答
  •  难免孤独
    2020-12-16 19:54

    Like this:

    #include 
    #include 
    
    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.

提交回复
热议问题