In an effort to better understand buffered streams in C++, I would like to write a simple program in which the std::cout
buffer is NOT flushed before termination. Since I have read that std::cout
is flushed on normal termination, I tried throwing a runtime error. I also avoided using std::endl
, as I understand that forces a flush. First attempt:
//file noflush.cpp
#include <iostream>
int main() {
std::cout << "Don't write me to the console!";
throw 0;
}
Compile with g++, call from terminal:
$ ./noflush
libc++abi.dylib: terminating with uncaught exception of type int
Don't write me to the console!Abort trap: 6
Even when I force a runtime error, it seems the buffer still gets flushed on termination. Is it possible to "strand" some data in the buffer, leaving it unwritten to the device?