In an application developed with Microsoft C/C++, the cout stream is buffered. In other words, information sent to the cout stream does not appear on the screen until its buffers are flushed. For Visual C++ 4.2 and later versions, this behavior only occurs when using the old iostream libraries. There are four methods to flush the cout buffer, as follows:
- Use the endl manipulator to insert a newline character into the output stream and flush the buffer. Use the insertion operator with the endl manipulator, as follows:
- Use the flush member function in the ostream class or the flush manipulator. The flush manipulator does not insert a newline character into the stream before it flushes the buffer. To call the flush member function, use code similar to the following: Use the insertion operator with the flush manipulator as follows:
- Read from the cin stream or write to the cerr or clog streams. Because these objects share the buffer with cout, each flushes the contents of the buffer before making any changes to it.
- Exit the program to flush all buffers currently in use.