Microsoft KB Archive/94227

= How to flush the cout buffer in Visual C++ =

Article ID: 94227

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.51
 * Microsoft Visual C++ 1.52 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-



This article was previously published under Q94227





SUMMARY
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: cout << ... << endl;   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: cout.flush; Use the insertion operator with the flush manipulator as follows: cout << ... << flush;  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.



MORE INFORMATION
Because cout is buffered, text that the application creates is not immediately displayed on the screen. In some circumstances, this behavior might be misinterpreted as an error in cout. For an example of an application that determines when to flush the cout buffer, see pages 377- 378 of the Microsoft C/C++ &quot;Class Libraries User's Guide&quot; for version 7.0.

For more information about flushing the cout stream with the endl manipulator, click the following article number to view the article in the Microsoft Knowledge Base:

Endl flushes the cout buffer immediately upon execution

Keywords: kbinfo kblangcpp KB94227

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.