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:
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() ,或以其他未指定手段达成调用 overflow() 的效果。

参数

(无)

返回值

成功写入的字符数。

注解

“以未指定手段达成 overflow() 的效果”容许无中间缓冲的大量 I/O :这是一些 iostream 的实现中, 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

参阅

调用 xsgetn()
(公开成员函数)