Namespaces
Variants

std::basic_streambuf<CharT,Traits>:: sputn, std::basic_streambuf<CharT,Traits>:: xsputn

From cppreference.net
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 xsputn() pptr ( ) == epptr ( ) 时总是调用 overflow() 实际上无需调用该函数

参见

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