std::basic_streambuf<CharT,Traits>::sputn, std::basic_streambuf<CharT,Traits>::xsputn
来自cppreference.com
< cpp | io | basic streambuf
std::streamsize sputn(const char_type* s, std::streamsize count ); | (1) | |
protected: virtualstd::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() 还是用其他方法达成同样的效果。
目录 |
[编辑]参数
(无)
[编辑]返回值
成功写入的字符数。
[编辑]注解
“用其他方法达成同样的效果”允许无中间缓冲的大量输入/输出:部分实现中 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<<"调用 sputn() 返回 "<< sz <<'\n'<<"输出序列包含 "<< s1.str(); std::istringstream s2; sz = s2.rdbuf()->sputn("This is a test", 14);std::cout<<"在输入流上调用 sputn() 返回 "<< sz <<'\n';}
输出:
调用 sputn() 返回 14 输出序列包含 This is a test 在输入流上调用 sputn() 返回 0
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 565 | C++98 | xsputn() 在 pptr()== epptr() 时始终会调用 overflow() | 不需要每次都实际调用它 |
[编辑]参阅
调用 xsgetn() (公开成员函数) |