我有下面的代码两次结束了,我不明白为什么结果恰好是像下面这样:COUT一个字符串流中损坏COUT(小例子)
#include <iostream>
#include <sstream>
using namespace std;
int main() {
std::stringstream s;
std::streambuf *backup;
backup = cout.rdbuf();
s << "Oh my god" << endl;
cout << "Before1" << endl;
cout << s.rdbuf();
cout << "After1" << endl;
cout << "Before2" << endl;
//s.seekg(0, std::ios_base::beg); // If that is in: After 2 is printed!
cout << s.rdbuf();
//cout.rdbuf(backup); // If that is in, also After 2 is printed!
cout << "After2" << endl;
}
输出:
Before1
Oh my god
After1
Before2
其余的地方??只有当我们取消注释上述行时才输出... 内部会发生什么?有人知道吗? =)会很有趣......
这可能是它的的libstdC++文档已经这样说了'__ostream_type& 运营商<<(__ streambuf_type * __sb);' “如果函数没有插入字符,failbit设置。”这看起来似乎是合理的,因为's.rdbuf()'会在第二次尝试打印时结束。 – nos 2012-07-11 21:42:09
哦,谢谢!这就是要点!我们可以在libstdC++ docu中读取哪些内容? – Gabriel 2012-07-11 21:42:47
@ Gabriel:这在标准的第27.7.3.6.3节中有描述。 – 2012-07-11 21:59:01