Namespaces
Variants
Views
Actions

std::basic_ostream::seekp

From cppreference.com
< cpp‎ | io‎ | basic ostream
Revision as of 20:35, 2 November 2012 by P12bot (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& seekp( pos_type pos );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& seekp( off_type off, std::ios_base::seekdir dir);
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Sets the output position indicator of the current associated streambuf object.

First, constructs a sentry object which checks the stream for errors and flushes the tie()'d output streams. (since C++11) Afterwards,

1) sets the output position indicator to absolute (relative to the beginning of the file) value pos by calling rdbuf()->pubseekoff(pos, std::ios_base::out). If the call returns (pos_type)-1, executes setstate(failbit).

2) sets the output position indicator to offset off relative to dir by calling rdbuf()->pubseekoff(off, dir, std::ios_base::out). Does not report errors.

Contents

Parameters

pos - absolute position to set the output position indicator to.
off - relative position to set the output position indicator to.
dir - defines base position to apply the relative offset to. It can be one of the following constants:
Constant Explanation
beg the beginning of a stream
end the ending of a stream
cur the current position of stream position indicator

Return value

*this

Exceptions

1) May throw std::ios_base::failure in case of failure, if exceptions() & failbit != 0.

2) Does not throw unless rdbuf()->pubseekoff() throws

Example

#include <sstream>
#include <iostream>
 
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!

See also

Template:cpp/io/basic ostream/dcl list tellpTemplate:cpp/io/basic istream/dcl list tellgTemplate:cpp/io/basic istream/dcl list seekg