public member function
<streambuf> <iostream>

std::basic_streambuf::pubsync

int pubsync();
Synchronize stream buffer
Calls the protected virtual member sync.

Member sync does nothing in basic_streambuf, but derived classes shall synchronize the contents pointed by the internal pointers with their associated sequences, if different (this effectively writes any unwritten characters on output buffers).

Parameters

none

Return Value

The default definition of sync in streambuf always returns zero, indicating success.
Derived classes can override this default behavior, and eventually return -1 to indicate failure.

Example

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;
}

In this example, the buffer is synchronized with the content of the file after the first sentence is put.

Data races

Modifies the stream buffer object.
Concurrent access to the same stream buffer object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream buffer is in a valid state (this also applies to standard derived classes).

See also