C++ streambuf::sputn() function
The C++ std::streambuf::sputn() function is used to write a sequence of characters to a buffer. It writes the specified number of characters from a given memory location to the stream buffer. It returns the number of characters successfully written.
Syntax
Following is the syntax for std::streambuf::sputn() function.
streamsize sputn (const char* s, streamsize n);
Parameters
- s − It indicates the pointer to the sequence of characters to be written.
- n − It indicates the number of characters to write.
Return Value
It returns the number of characters written.
Exceptions
If an exception is thrown, the stream buffer is in a valid state.
Data races
It modifies the stream buffer object.
Example 1
In the following example, we are going to consider the basic usage of the sputn() function.
#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;
}
Output
Output of the above code is as follows −
TP
Example 2
Consider the following example, where we are going to write the part of the string.
#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;
}
Output
Following is the output of the above code −
Hi
streambuf.htm
Advertisements