News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ tips:IO - Unit-buffering cout

Started by ben2ong2, October 06, 2006, 11:09:12 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Robert DeLine)

How do I get cout to be unbuffered like cerr is?  (i.e. What's
the iostream equivalent of the old C expression
setbuf(stdout,NULL); ?)

In general, what's a good source of information about iostreams?
(The chapter in Stroustrup's 2nd Ed. is not comprehensive.)


RESPONSE: You are not allowed to view links. Register or Login (Steve Clamage)

First of all, cerr isn't unbuffered, it is unit-buffered. This is a
useful compromise which flushes the buffer after each insertion,
rather than after each character.  You can simply set the unit-buffer
flag (ios::unitbuf) in the stream to get this effect.  (If you do
unformatted output, you still have to flush manually.)

Alternatively, you can use the setbuf member function of the buffer
class:
   cout.rdbuf()->setbuf(0,0);
This is not guaranteed actually to unbuffer the stream, depending
on a number of implementation details.

Finally, you can create an unbuffered ofstream attached to file
descriptor 1 (on most systems), and use that instead of cout.

At the moment, the best source of detailed data on iostreams is the
USL C++ library documentation, which you can buy directly from USL
if it didn't come with your implementation.

Addison-Wesley will be publishing a book by Steven Teale on iostreams
sometime this year.  I don't know the exact title of the book or the
publication date.


RESPONSE: You are not allowed to view links. Register or Login (Steve Vinoski)

I believe the title is "IOStreams Cookbook" and last I heard it will
be available in June.  The ISBN is 0-201-59641-X.  Addison-Wesley can
be reached at (617)944-3700.
You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login