std::basic_streambuf<CharT,Traits>::sputn, std::basic_streambuf<CharT,Traits>::xsputn
来自 cppreference.cn
< cpp | io | basic streambuf
| std::streamsize sputn( const char_type* s, std::streamsize count ); |
(1) | |
| protected: virtual std::streamsize xsputn( const char_type* s, std::streamsize count ); |
(2) | |
1) 调用最派生类的 xsputn(s, count)。
2) 从首元素为 s 所指向的字符数组,写 count 个字符到输出序列。如同以重复调用 sputc() 一般写入字符。当写入了 count 个字符,或对 sputc() 的调用会返回 Traits::eof() 时,写入停止。
若放置区变满(pptr() == epptr()),则是否实际调用 overflow(),或以其他手段达成其效果,是未指定的。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
成功写入的字符数。
[编辑] 注意
“以其他手段达成”允许无中间缓冲的大块 I/O:这是 std::ofstream::write() 在一些实现中直接将指针传递给适当系统调用的方式。
[编辑] 示例
运行此代码
#include <iostream> #include <sstream> int main() { std::ostringstream s1; std::streamsize sz = s1.rdbuf()->sputn("This is a test", 14); s1 << '\n'; std::cout << "The call to sputn() returned " << sz << '\n' << "The output sequence contains " << s1.str(); std::istringstream s2; sz = s2.rdbuf()->sputn("This is a test", 14); std::cout << "The call to sputn() on an input stream returned " << sz << '\n'; }
输出
The call to sputn() returned 14 The output sequence contains This is a test The call to sputn() on an input stream returned 0
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
|---|---|---|---|
| LWG 565 | C++98 | 若 pptr() == epptr(),则 xsputn() 始终调用 overflow() |
它实际上不必被调用 |
[编辑] 参阅
| 调用 xsgetn() (公开成员函数) |