cout重定向

在网上查了一会,发现了新的东东:iostream的重定向

原本打算看看c++在console中,能否把cin,cout流中的内容可以不用经过文件操作,能够像字符串一样来使用,

良久,未果,但是发现了重定向这个东东。

笔记一下:

/*//Fuction prototype from msdn
basic_streambuf<Elem, Traits> *rdbuf( ) const; basic_streambuf<Elem, Traits> *rdbuf( basic_streambuf<E, T> *_Sb );  
*/ #include <iostream> #include <fstream> using namespace std; int main() { ofstream outfile("outfile.txt"); streambuf *oldbuf = cout.rdbuf(); // back up cout's streambuf cout.rdbuf(outfile.rdbuf());// get file's streambuf,// assign streambuf to cout cout<<"This string is doomed to be written to file."; //written to the file cout.rdbuf(oldbuf);//// restore cout's original streambuf - the screen cout<<"Written to the screen.\n"; return 0; }

我试了一下

string instr;

instr.rdbuf();

哎。。行不通,没有这种写好的函数提供给你。

问题待定。。

posted @ 2012-04-12 21:29  KelvinDesus  阅读(973)  评论(0)    收藏  举报