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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 565 | C++98 | 如果 pptr() == epptr(),则 xsputn() 总是调用 overflow() |
它实际上不需要被调用 |
[编辑] 参见
调用 xsgetn() (公共成员函数) |