【发布时间】:2011-08-26 04:46:02
【问题描述】:
在his answer 中,特别是在linked Ideone example 中,@Nawaz 展示了如何更改cout 的缓冲区对象以写入其他内容。这让我想到利用它来准备来自cin 的输入,通过填写其streambuf:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
streambuf *coutbuf = cout.rdbuf(cin.rdbuf());
cout << "this goes to the input stream" << endl;
string s;
cin >> s;
cout.rdbuf(coutbuf);
cout << "after cour.rdbuf : " << s;
return 0;
}
但这并不像预期的那样工作,或者换句话说,它失败了。 :| cin 仍然需要用户输入,而不是从提供的 streambuf 中读取。有没有办法让这个工作?
【问题讨论】:
-
你是问如何过滤
cin? -
@Gabe:嗯,不,我想为 cin 提供一个准备好的输入,所以它不需要用户输入,而是从提供的内容中读取。
标签: c++ redirect iostream cin streambuf