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.

引數

s
指向要寫入的字元序列的指標。
成員型別 char_type流緩衝區(第一個類模板引數)中字元的型別。
n
要寫入的字元數。
這應該是一個非負值。
streamsize 是一個帶符號整型。

返回值

寫入的字元數。
streamsize 是一個帶符號整型。

示例

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.

資料競爭

訪問 s 指向的陣列中最多前 n 個字元。
修改*流緩衝區*物件。
對同一陣列或同一流緩衝區物件的併發訪問可能會引入資料爭用。

異常安全

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

無效引數會導致未定義行為

另見