Conventionally, std::cin, std::cout are buffererd and std::cerr is not buffered. Unbuffered streams are written to device immediately. In general, ofstreams are buffered. You can make a stream unbuffered by invoking setbuf(0,0). For example, ofstream of; of.setbuf(0,0); // makes it unbuffered. You can force a buffered stream to flush the contents using std::endl. Other interesting thing is to tie an buffered output stream with a buffered input stream. What this means is, whenever you want to accept an input from the input stream, the output stream 'tied' to it is flushed automatically. For example, ifstream in; // a buffered input stream. ofstream out; // a buffered output stream. and in and out are tied together. then, out << "data 1" << "data 2" << ..... << "data N"; // may or may not occur on screen or file. ... in >> somevar; // out will be flushed before somevar is input. You do this by invoking: in.tie(&out)
A blog on various topics in C++ programming including language features, standards, idioms, design patterns, functional, and OO programming.