public member function
<streambuf> <iostream>

std::streambuf::pubsetbuf

streambuf* pubsetbuf (char* s, streamsize n);
Set buffer array
Calls the protected virtual member setbuf with the same arguments s and n.

Member setbuf does nothing in streambuf, but derived classes may override it to influence the object in a specific way for each derived class: both filebuf and stringbuf override this virtual member function to set the array of n characters pointed by s as the internal character sequence to be used by the stream buffer object (see filebuf::setbuf and stringbuf::setbuf).

Parameters

s, n
Arguments that may be used by overriding functions in derived classes.
streamsize is a signed integral type.

Return Value

In streambuf, it always returns this.

Example

1
2
3
4
5
6
7
8
9
10
11
12
// set character buffer (pubsetbuf)
#include <fstream>      // std::fstream

int main () {
  char mybuffer [512];
  std::fstream filestr;
  filestr.rdbuf()->pubsetbuf(mybuffer,512);

  // operations with file stream here.

  return 0;
}

The code in this example sets a new buffer of 512 characters for filestr's stream buffer object.

Data races

Both this call and further calls to other member functions may access and/or modify the first n characters in the array pointed by s.
Modifies the stream buffer object.
Concurrent access to the same array or 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).

Invalid arguments cause undefined behavior.

See also