名稱空間
變體
操作

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(),或以其他手段達成其效果,是未指定的。

目錄

[編輯] 引數

(無)

[編輯] 返回值

成功寫入的字元數。

[編輯] 注意

“以其他手段達成”允許無中間緩衝的大塊 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()
(公開成員函式) [編輯]