The C++ std::ostream::seekp() function is used to move the output position indicator of a stream. It allows repositioning the write pointer within the stream, enabling data to be written to specific locations. This function can take either an absolute position or an offset relative to a given position (beginning, end, or current).
SyntaxFollowing is the syntax for std::ostream::seekp() function.
ostream& seekp (streampos pos); or ostream& seekp (streamoff off, ios_base::seekdir way);Parameters
It returns the ostream object (*this).
ExceptionsIf an exception is thrown, the object is in a valid state.
Data racesModifies the stream object and concurrent access to the same stream object may cause data races.
ExampleIn the following example, we are going to consider the basic usage of the seekp() function.
#include <iostream> #include <sstream> int main() { std::ostringstream a; a << "W LCOME"; a.seekp(1); a << "E"; std::cout << a.str() << std::endl; return 0; }Output
Output of the above code is as follows −
WELCOMEExample
Consider the following example, where we are going to use the seekp() to overwrite the part of string.
#include <iostream> #include <sstream> int main() { std::ostringstream a; a << "1231114567"; a.seekp(3); a << "XYZ"; std::cout << a.str() << std::endl; return 0; }Output
Following is the output of the above code −
123XYZ4567Example
Let's look at the following example, where we are going to append the data to the end.
#include <iostream> #include <sstream> int main() { std::ostringstream a; a << "Welcone To"; a.seekp(0, std::ios::end); a << " TutorialsPoint"; std::cout << a.str() << std::endl; return 0; }Output
If we run the above code it will generate the following output −
Welcone To TutorialsPoint
ostream.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4