Buffered stream cout?

In my C++ book, it says that cout is a buffered stream. This means that the data will be written to the buffer and will be printed when it reaches the endl marker, a cin statement, or explicitly flushed using cout.flush(). Later I did a test to see if this was true:

1
2
  //Pseudo-code, can't compile
cout << "This won't print out";


But, it did print out. Why does this happen? I didn't flush the buffer or use endl, but it still printed to the screen
Last edited on
another way is to call cout.flush(), but it already prints it
There are several things which may affect this behaviour. Try:
 
    cout.sync_with_stdio(false);
cout is flushed when your program terminates and the ostream for cout is destroyed.

yes, but that means that this shouldn't print out:

 
cout << test;


but it still prints out. Why does this occur?
@Chervil

I tried that and it worked. But, do I have to do this every time whenever I use streams? What I mean is, is it set on "true" by default?
Last edited on
that means that this shouldn't print out:

No, you're missing my point.

 
cout << test;


That copies the contents of test to cout's stream buffer. It does not appear on the console at this point.

Now, you exit the program. cout's destructor is called. Part of the process of destructing cout is a call to flush(). This causes the contents of cout's stream buffer (test) to be written to the console.

The book isn't telling you the whole truth. cout isn't buffered by the STL streams library -- it is buffered by the underlying terminal. On Windows, the console display is not typically buffered for writes, so the stuff you cout shows up right away.

Hope this helps.
@AbstractionAnon
I understand that. I'm saying that it prints out even before ostream destructor is called, I mean when the line is written it prints out

EDIT:
@Duoas
Thanks, that makes a lot more sense

EDIT 2:
I figured how to make it work, thanks to @Chervil. I just make a call to cout.sync_with_stdio() and set it to "false".
Last edited on
Every C++ stream uses an associated stream buffer object to perform buffering.

When std::cout is constructed, it uses the stream buffer associated with the object stdout, declared in <cstdio>.
By default, operations on std::cout can be freely mixed with <cstdio> output functions like std::printf().

In practical terms, synchronization usually means that a standard iostream object and a standard stdio object share a buffer. - IS


If std::ios_base:: sync_with_stdio(false) is called (before any input or output operations on the standard streams), the standard C++ streams operate independently of the standard C streams (ie. they switch to their own separate stream buffers).

std::cout is just an object of type std::ostream; except for the way in which it is constructed, there is nothing special about it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <fstream>
#include <utility>

int main()
{
    // at this point, std::cout shares a common buffer with std::printf

    std::ios_base::sync_with_stdio( false ) ; 
    // std::cout now has its own separate buffer. this buffer too, when flushed, 
    // writes the characters to the device associated with stdout 

    int i = 0 ;
    std::cout << ++i << ". this is written to stdout\n" ;

    std::cout << std::unitbuf ; // make std::cout flush the buffer after every output 
    std::cout << ++i << ". this too is written to stdout\n" ;

    // switch the buffer associated with std::cout to a file buffer
    std::filebuf fbuf ;
    fbuf.open( "temp.txt", std::ios::out );
    auto oldbuf = std::cout.rdbuf( std::addressof( fbuf ) ) ; 

    std::cout << ++i << ". this is written to the file\n" ;
    std::cout << std::nounitbuf ; // turn off unitbuf 
    std::cout << ++i << ". this too is written to the file\n" ;

    std::cout.rdbuf( oldbuf ); // restore std::cout's earlier buffer 
    std::cout << ++i << ". output is now back to stdout\n" << std::flush ;

    {
        // construct an ostream which shares the buffer with std::cout
        std::ostream my_stream( oldbuf ) ; 
        my_stream << ++i << ". this output too is to to stdout\n" << std::flush ;

        // my_stream and std::cout are 'synchronized'; write 'n. hello m. world' to stdout
        std::cout << ++i << ". hello " ;
        my_stream << ++i << ". world\n" ;
    }
}

http://coliru.stacked-crooked.com/a/a6788441e8bd6d29
Topic archived. No new replies allowed.