分类: C/C++
2007-10-15 19:47:03
streambuf* rdbuf ( ) const; |
Get/set the associated stream buffer
The first syntax returns the stream buffer object associated with the stream.
The second syntax associates the stream with sb and returns the stream buffer object previously associated with the stream. In this case, the buffer's control state is set to goodbit as if a call to member .
另外在CSDN上面以为老兄的帖子中,还绝伦一个例子,并提出了两个需要注意的地方,我感觉比较重要,就贴过来了。
#include
#include
int main()
{
std::ofstream log("foo.log");
std::streambuf *oldbuf = std::cout.rdbuf(log.rdbuf());
std::cout << "输出到标准输出,但实际输出到了foo.log文件中\n";
log << "输出到文件,虽然将cout重定向到了log,但不影响log本身的使用\n";
// 恢复流缓冲区
std::cout.rdbuf(oldbuf);
}
注意:
1.cout可以调用rdbuf来替换缓冲区,但log不能通过调用rdbuf来替换缓冲区。因为ofstream的rdbuf并没有同样的功能。
basic_streambuf* rdbuf ( basic_streambuf sb );