std::basic_ostream::seekp(3)
π NAME
std::basic_ostream::seekp β okay bestie, this is the function that sets the output position indicator, and it is lowkey iconic. Manual name drop: C++ Standard Libary (yes, that spelling is in the source, we respect the lore), and the page is std::basic_ostream::seekp(3).
πΈ SYNOPSIS
basic_ostream& seekp( pos_type pos ); (1)
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir ); (2)
Sets the output position indicator of the current associated streambuf object.
Behaves as UnformattedOutputFunction (except, since C++11, without actually performing output). After constructing and checking the sentry object:
- if
fail() != true, sets the output position indicator to absolute (relative to the beginning of the file) valueposby callingrdbuf()->pubseekpos(pos, std::ios_base::out). In case of failure, callssetstate(std::ios_base::failbit). - if
fail() != true, sets the output position indicator to offsetoffrelative todirby callingrdbuf()->pubseekoff(off, dir, std::ios_base::out). In case of failure, callssetstate(std::ios_base::failbit).
β¨ PARAMETERS
posβ absolute position to set the output position indicator to.offβ relative position (positive or negative) to set the output position indicator to.dirβ defines the base position to apply the relative offset to. It can be one of the following constants:begβ the beginning of a streamendβ the ending of a streamcurβ the current position of stream position indicator
π«Ά RETURN VALUE
*this β she said βIβm the main characterβ and returned herself.
π EXCEPTIONS
1,2) May throw std::ios_base::failure in case of failure, if exceptions() & failbit != 0.
π EXAMPLE
// Run this code
#include <iostream>
#include <sstream>
int main()
{
std::ostringstream os("hello, world");
os.seekp(7);
os << 'W';
os.seekp(0, std::ios_base::end);
os << '!';
os.seekp(0);
os << 'H';
std::cout << os.str() << '\n';
}
π OUTPUT
Hello, World!
π§ DEFECT REPORTS
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
Table columns, for the record: DR, Applied to, Behavior as published, Correct behavior.
LWG 129β applied to C++98. Behavior as published: there was no way to indicate a failure. Correct behavior: setsfailbiton failure.LWG 136β applied to C++98. Behavior as published:seekpcould set the input stream. Correct behavior: only sets the output stream.LWG 537β applied to C++98. Behavior as published: 1. the type ofposwaspos_type&; 2. the type ofoffwasoff_type&. Correct behavior: 1. corrected topos_type; 2. corrected tooff_type.LWG 2341β applied to C++98. Behavior as published: the resolution of LWG issue 129 for overload (2) was removed. Correct behavior: restored.
π SEE ALSO
tellpβ returns the output position indicator (public member function)tellgβ returns the input position indicator (public member function ofstd::basic_istream<CharT,Traits>)seekgβ sets the input position indicator (public member function ofstd::basic_istream<CharT,Traits>)
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::basic_ostream::seekp(3) β thatβs the whole vibe, mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms