Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strstreambuf::seekpos

      From cppreference.com
      <cpp‎ |io‎ |strstreambuf
       
       
       
       
      protected:

      virtual pos_type seekpos( pos_type sp,
                               std::ios_base::openmode which=

                                   std::ios_base::in|std::ios_base::out);
      (deprecated in C++98)
      (removed in C++26)

      Repositionsstd::basic_streambuf::gptr and/orstd::basic_streambuf::pptr, if possible, to the position indicated bysp.

      Ifstd::ios_base::in is set inwhich, attempts to repositiongptr() (the next pointer in the get area). Ifstd::ios_base::out is set inwhich, attempts to repositionpptr() (the next pointer in the put area). If neither bit is set inwhich, the operation fails.

      Each next pointer is repositioned as follows:

      • If the next pointer is null, the operation fails.
      • Otherwise, the new offsetnewoff (of typeoff_type) is determined by callingsp.offset(). Ifnewoff is negative, out of bounds of the buffer, or invalid, the operation fails.
      • Otherwise, the next pointer is assigned as if bygptr()= eback()+ newoff orpptr()= pbase()+ newoff.

      Contents

      [edit]Parameters

      sp - stream position, such as one obtained byseekoff() orseekpos()
      which - defines whether the input sequences, the output sequence, or both are affected. It can be one or a combination of the following constants:
      Constant Explanation
      in affect the input sequence
      out affect the output sequence

      [edit]Return value

      The resultant offset converted topos_type on success orpos_type(off_type(-1)) on failure.

      [edit]Notes

      seekpos() is called bystd::basic_streambuf::pubseekpos(), which is called by the single-argument versions ofstd::basic_istream::seekg() andstd::basic_ostream::seekp().

      [edit]Example

      Run this code
      #include <cstring>#include <iostream>#include <strstream> struct mybuf:std::strstreambuf{    mybuf(constchar* str):std::strstreambuf(str,std::strlen(str)){}     pos_type seekpos(pos_type sp,std::ios_base::openmode which){std::cout<<"Before seekpos("<< sp<<"), size of the get area is "<< egptr()- eback()<<" with "<< egptr()- gptr()<<" read positions available.\n";         pos_type rc= std::strstreambuf::seekpos(sp, which); std::cout<<"seekpos() returns "<< rc<<".\nAfter the call, "<<"size of the get area is "<< egptr()- eback()<<" with "<< egptr()- gptr()<<" read positions available.\n"; return rc;}}; int main(){    mybuf buf("12345");std::iostream stream(&buf);    stream.seekg(2);}

      Output:

      Before seekpos(2), size of the get area is 5 with 5 read positions available.seekpos() returns 2.After the call, size of the get area is 5 with 3 read positions available.

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 55C++98seekpos returned an undefined
      invalid stream position on failure
      pos_type(off_type(-1))
      is returned on failure

      [edit]See also

      [virtual]
      repositions the next pointer in the input sequence, output sequence, or both, using relative addressing
      (virtual protected member function)[edit]
      [virtual]
      repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
      (virtual protected member function ofstd::basic_streambuf<CharT,Traits>)[edit]
      [virtual]
      repositions the next pointer in the input sequence, output sequence, or both using absolute addressing
      (virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)[edit]
      [virtual]
      repositions the file position, using absolute addressing
      (virtual protected member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/strstreambuf/seekpos&oldid=170654"

      [8]ページ先頭

      ©2009-2025 Movatter.jp