public member function
<streambuf> <iostream>
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 是一个带符号整型。
示例
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 个字符。
修改*流缓冲区*对象。
对同一数组或同一流缓冲区对象的并发访问可能会引入数据争用。
异常安全
基本保证:如果抛出异常,则流缓冲区处于有效状态(这也适用于标准派生类)。
无效参数会导致未定义行为。