Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_ostream<CharT,Traits>::seekp

      From cppreference.com
      <cpp‎ |io‎ |basic ostream
       
       
       
       
      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 associatedstreambuf object.

      Behaves asUnformattedOutputFunction (except without actually performing output). After constructing and checking the sentry object,

      (since C++11)
      1) iffail()!=true, sets the output position indicator to absolute (relative to the beginning of the file) valuepos by callingrdbuf()->pubseekpos(pos,std::ios_base::out). In case of failure, callssetstate(std::ios_base::failbit).
      2) iffail()!=true, sets the output position indicator to offsetoff relative todir by callingrdbuf()->pubseekoff(off, dir,std::ios_base::out). In case of failure, callssetstate(std::ios_base::failbit).

      Contents

      [edit]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 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

      [edit]Return value

      *this

      [edit]Exceptions

      1,2) May throwstd::ios_base::failure in case of failure, ifexceptions()& failbit!=0.

      [edit]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!

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 129C++98there was no way to indicate a failuresetsfailbit on failure
      LWG 136C++98seekp could set the input streamonly sets the output stream
      LWG 537C++981. the type ofpos waspos_type&
      2. the type ofoff wasoff_type&
      1. corrected topos_type
      2. corrected tooff_type
      LWG 2341C++98the resolution ofLWG issue 129 for overload (2) was removedrestored

      [edit]See also

      returns the output position indicator
      (public member function)[edit]
      returns the input position indicator
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      sets the input position indicator
      (public member function ofstd::basic_istream<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ostream/seekp&oldid=158551"

      [8]ページ先頭

      ©2009-2025 Movatter.jp