public member function
<streambuf> <iostream>

std::streambuf::pubsync

int pubsync();
同步流緩衝區
呼叫受保護的虛成員 sync

sync 的成員函式在 streambuf 中不做任何事情,但派生類應該將“內部指標”指向的內容與它們的“關聯序列”同步(這會有效地將輸出緩衝區中的任何未寫入字元寫入)。

引數



返回值

sync 的預設定義在 streambuf 中,總是返回零,表示成功。
派生類可以重寫此預設行為,並最終返回 -1 來表示失敗。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// pubsync member
#include <iostream>     // std::cout, std::streambuf
#include <fstream>      // std::ofstream

int main () {
  std::ofstream ostr ("test.txt");
  if (ostr) {
    std::streambuf * pbuf = ostr.rdbuf();

    pbuf->sputn ("First sentence\n",15);
    pbuf->pubsync();
    pbuf->sputn ("Second sentence\n",16);

    ostr.close();
  }
  return 0;
}

在此示例中,在寫入第一個句子後,緩衝區與檔案內容同步。

資料競爭

修改*流緩衝區*物件。
同時訪問同一*流緩衝區*物件可能會導致資料競爭。

異常安全

基本保證:如果丟擲異常,則流緩衝區處於有效狀態(這也適用於標準派生類)。

另見