public member function
<streambuf> <iostream>

std::basic_streambuf::sputn

streamsize sputn (const char_type* s, streamsize n);
Put sequence of characters
Calls the protected virtual member xsputn with the same arguments s and n.

The default definition of xsputn in basic_streambuf writes characters from the array pointed to by s into the controlled output sequence, until either n characters have been written or the end of the output sequence is reached.

Parameters

s
Pointer to the sequence of characters to be written.
Member type char_type is the type of the characters in the stream buffer (the first class template parameter).
n
Number of characters to write.
This shall be a non-negative value.
streamsize is a signed integral type.

Return Value

The number of characters written.
streamsize is a signed integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// sputn() example
#include <iostream>     // std::streambuf
#include <fstream>      // std::ofstream

int main () {
  const char sentence[]= "Sample sentence";

  std::ofstream ostr ("test.txt");
  if (ostr) {
    std::streambuf * pbuf = ostr.rdbuf();
    pbuf->sputn (sentence,sizeof(sentence)-1);
    ostr.close();
  }

  return 0;
}


This example writes a sentence to a file using streambuf's member sputn.

Data races

Accesses up to all of 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