C++ streambuf - sputn


Advertisements

Description

It is used to put sequence of characters and calls the protected virtual member xsputn with the same arguments s and n.

Declaration

Following is the declaration for std::streambuf::sputn.

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

Parameters

  • s − The pointer to the sequence of characters to be written.

  • n − The number of characters to write.

Return Value

It returns the number of characters written.

Exceptions

Basic guarantee − if an exception is thrown, the stream buffer is in a valid state.

Data races

It modifies the stream buffer object.

Example

In below example explains about std::streambuf::sputn.

#include <iostream>     
#include <fstream>      

int main () {
   const char sentence[]= "Sample sentence";

   std::ofstream ostr ("test.txt");
   if (ostr) {
      std::streambuf * pbuf = ostr.rdbuf();
      pbuf->sputn (sentence,sizeof(sentence)-1);
      ostr.close();
   }
   return 0;
}
streambuf.htm
Advertisements