Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strstreambuf::seekoff

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

      virtual pos_type seekoff( off_type off,
                                ios_base::seekdir way,

                                ios_base::openmode which= ios_base::in| 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 that corresponds to exactlyoff characters from beginning, end, or current position of the get and/or put area of the buffer.

      • Ifwhich includesios_base::in and this buffer is open for reading, then repositions the read pointerstd::basic_streambuf::gptr inside the get area as described below.
      • Ifwhich includesios_base::out and this buffer is open for writing, then repositions the write pointerstd::basic_streambuf::pptr inside the put area as described below.
      • Ifwhich includes bothios_base::in andios_base::out and the buffer is open for both reading and writing, andway is eitherios_base::beg orios_base::end, then repositions both read and write pointers as described below.
      • Otherwise, this function fails.

      If the pointer (eithergptr orpptr or both) is repositioned, it is done as follows:

      1) If the pointer to be repositioned is a null pointer and the new offsetnewoff would be non-zero, this function fails.
      2) The new pointer offsetnewoff of typeoff_type is determined
      a) ifway== ios_base::beg, thennewoff is zero
      b) ifway== ios_base::cur, thennewoff is the current position of the pointer (gptr()- eback() orpptr()- pbase())
      c) ifway== ios_base::end, thennewoff is the length of the entire initialized part of the buffer (if overallocation is used, the high watermark pointer minus the beginning pointer)
      3) Ifnewoff+ off is negative or out of bounds of the initialized part of the buffer, the function fails
      4) Otherwise, the pointer is assigned as if bygptr()= eback()+ newoff+ off orpptr()= pbase()+ newoff+ off

      Contents

      [edit]Parameters

      off - relative position to set the next pointer(s) to
      way - 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
      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

      pos_type(newoff) on success,pos_type(off_type(-1)) on failure and if pos_type cannot represent the resulting stream position.

      [edit]Example

      Run this code
      #include <iostream>#include <strstream> int main(){char a[]="123";std::strstream ss(a, sizeof a);// in/outstd::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // absolute positioning both pointers    ss.rdbuf()->pubseekoff(1,std::ios_base::beg);// move both forwardstd::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // try to move both pointers 1 forward from current positionif(-1== ss.rdbuf()->pubseekoff(1,std::ios_base::cur))std::cout<<"moving both pointers from current position failed\n";std::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n'; // move the write pointer 1 forward, but not the read pointer// can also be called as ss.seekp(1, std::ios_base::cur);    ss.rdbuf()->pubseekoff(1,std::ios_base::cur,std::ios_base::out);std::cout<<"put pos = "<< ss.tellp()<<" get pos = "<< ss.tellg()<<'\n';     ss<<'a';// write at put positionstd::cout<<"Wrote 'a' at put position, the buffer is now: '";std::cout.write(a, sizeof a);std::cout<<"'\n"; char ch;    ss>> ch;std::cout<<"reading at get position gives '"<< ch<<"'\n";}

      Output:

      put pos = 0 get pos = 0put pos = 1 get pos = 1moving both pointers from current position failedput pos = 1 get pos = 1put pos = 2 get pos = 1Wrote 'a' at put position, the buffer is now: '12a'reading at get position gives '2'

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 55C++98seekoff 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 absolute addressing
      (virtual protected member function)[edit]
      [virtual]
      repositions the next pointer in the input sequence, output sequence, or both, using relative 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 relative addressing
      (virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)[edit]
      [virtual]
      repositions the file position, using relative addressing
      (virtual protected member function ofstd::basic_filebuf<CharT,Traits>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/strstreambuf/seekoff&oldid=170653"

      [8]ページ先頭

      ©2009-2025 Movatter.jp