1

I have seen cout.rdbuf() in for example here. This implies, the stream cout have a stream buffer associated with it, which is non empty before we flush it.

But, how can I ever peek into cout's stream buffer for cout before it's flushed? Ie

cout << "I want to read this before this get flushed";
cout.UnknownFunction();  //this would save the buffer into a string variable
cout << flush;

But in current form of the code, everything will be flushed onto the screen after the first line..

So, what kind of construct allows me to peek inside the cout buffer?

PS. im running VC++ 2010 on windows7

Linus Kleen
  • 33,871
  • 11
  • 91
  • 99
Sida Zhou
  • 3,529
  • 2
  • 33
  • 48
  • Read a [reference of the C++ stream system](http://en.cppreference.com/w/cpp/io)? – Some programmer dude Mar 30 '13 at 12:41
  • I tried and failed.. Maybe need to try more – Sida Zhou Mar 30 '13 at 13:27
  • The (now deleted) answer by David is the way to get the buffer. However if it's flushed it will be empty. You can check the [`ios_base` format flags](http://en.cppreference.com/w/cpp/io/ios_base/fmtflags) to see if `unitbuf` is set, in that case the output is flushed after every output operation. – Some programmer dude Mar 30 '13 at 13:51

2 Answers2

0

I think that this doesn't flush after the first line, but I'm absolutely NOT sure. I experienced that endl flushes, but the others don't, it's possible that too much character automatically flushes, but I don't know. I created (accidentally) a program like this (in short):

cout << "x";
while (true) {}

The program ran this, and the output would be debug, but it haven't written anything for me, so I thought the program doesn't get there...

radl
  • 300
  • 3
  • 12
  • This would be called "line buffering". It means that the buffer will not be flushed until a newline is inserted, the first or Nth time. – Ed S. Mar 31 '13 at 02:29
0

Following link is closely related to this topic C++ buffered stream IO

(But I'm still not sure how/when to get cout.rdbuf() onto a string.)

Community
  • 1
  • 1
Sida Zhou
  • 3,529
  • 2
  • 33
  • 48