共用方式為


緩衝的效果

下列範例顯示已緩衝的效果。 您可能預期程式會列印 please wait,則為等待 5 秒,然後執行。 因為要緩衝輸出,不過,它不一定會運作方式。

// effects_buffering.cpp
// compile with: /EHsc
#include <iostream>
#include <time.h>
using namespace std;

int main( )
{
   time_t tm = time( NULL ) + 5;
   cout << "Please wait...";
   while ( time( NULL ) < tm )
      ;
   cout << "\nAll done" << endl;
}

訊息時,是出現時,要以邏輯方式將程式工作, cout 物件必須清空自己。 若要清除 ostream 物件,將它 flush 操作工具:

cout << "Please wait..." << flush;

這個步驟清除緩衝區,以確保訊息列印在等候。 您也可以使用 endl 操作,清除緩衝區並輸出重設為一組,或是使用 cin 物件。 這個物件 ( cerrclog 物件) 通常會繫結至 cout 物件。 因此,對 cin 的所有使用 (或 cerrclog 物件) 清除 cout 物件。

請參閱

參考

輸出資料流