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:
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.
@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".
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.
#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" ;
}
}