1

I wanted to know does the output buffer automatically get flushed and emptied every time cout is used. If it is not flushed, is there any way I can "check the contents" of the output buffer? I am referring to cases where you only use cout and no endl is involved.

Consider the following code:

cout << "Hello, how are you?";    //Without using endl

I used stringstream to check how flush affects the buffer. I have the following codes, but why is the output still showing "GoodDay" even though I already flushed it?

string str;
stringstream ss;
ss << "GoodDay";
ss << flush;
ss >> str;
cout << str;
  • If you use either `std::endl` or write more the buffered output can actually hold, it's flushed yes. As for your question about checking contents, may be having a look at [`std::ios_base::register_callback`](http://en.cppreference.com/w/cpp/io/ios_base/register_callback) is useful. [My answer here](http://stackoverflow.com/questions/22263636/fstream-c-automatic-line-addition/22263699#22263699) might also give you some insight. – πάντα ῥεῖ Mar 13 '14 at 20:08
  • So, if I don't use std::endl and do not write more than the buffered output can hold, does the output buffer still get flushed when I use cout? Thanks for your reply. – user3347939 Mar 13 '14 at 20:15
  • _'does the output buffer still get flushed when I use cout?'_ Nope ... – πάντα ῥεῖ Mar 13 '14 at 20:16
  • Why do you need to know that - a stream might be unbuffered (The remaining content of a buffer is flushed if the stream runs out of scope) –  Mar 13 '14 at 20:18
  • `std::cout << "whatever" << std::flush;` http://stackoverflow.com/a/14107357/425871 – Steve Mar 13 '14 at 20:18

1 Answers1

0

stringstream is not supposed to loose its content on flush. Actually the flush manipulator has no meaning to stringstream whatsoever, as any input is immediately written to the buffer wrapped by stringstream. That's why you still see "GoodDay".

Matthias
  • 1,296
  • 8
  • 17