C++ streambuf::sputn() 函式



C++ 的std::streambuf::sputn()函式用於將一系列字元寫入緩衝區。它將給定記憶體位置中的指定數量的字元寫入流緩衝區。它返回成功寫入的字元數。

語法

以下是 std::streambuf::sputn() 函式的語法。

streamsize sputn (const char* s, streamsize n);

引數

  • s - 指示要寫入的字元序列的指標。
  • n - 指示要寫入的字元數。

返回值

它返回寫入的字元數。

異常

如果丟擲異常,則流緩衝區處於有效狀態。

資料競爭

它修改流緩衝區物件。

示例 1

在以下示例中,我們將考慮 sputn() 函式的基本用法。

#include <iostream>
#include <sstream>
int main() {
   std::stringbuf x;
   const char * y = "TP";
   x.sputn(y, 2);
   std::cout << x.str() << std::endl;
   return 0;
}

輸出

以上程式碼的輸出如下:

TP

示例 2

考慮以下示例,我們將寫入字串的一部分。

#include <iostream>
#include <sstream>
int main() {
   std::stringbuf x;
   const char * y = "Hi, Welcome";
   x.sputn(y, 2);
   std::cout << x.str() << std::endl;
   return 0;
}

輸出

以下是以上程式碼的輸出:

Hi
streambuf.htm
廣告