public member function
<streambuf> <iostream>

std::streambuf::pubsync

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

Member sync does nothing in 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

A value of -1 indicates failure.
The default definition in streambuf always returns -1.
Derived classes that override sync, shall return some other value (such as zero) to indicate success.

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