Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_filebuf<CharT,Traits>::seekpos

      From cppreference.com
      <cpp‎ |io‎ |basic filebuf
       
       
       
       
      protected:

      virtual pos_type seekpos( pos_type sp,

                               std::ios_base::openmode which=std::ios_base::in|std::ios_base::out);

      Repositions the file pointer, if possible, to the position indicated bysp. If the associated file is not open (is_open()==false), fails immediately.

      Reposition performs as follows:

      1) If the file is open for writing, writes the put area and any unshift sequences required by the currently imbued locale, usingoverflow().
      2) Repositions the file pointer, as if by callingstd::fsetpos().
      3) If the file is open for reading, updates the get area if necessary.

      Ifsp was not obtained by callingseekoff() orseekpos() on the same file, the behavior is undefined.

      Contents

      [edit]Parameters

      sp - file position obtained byseekoff() orseekpos() called earlier on the same file
      which - defines which of the input and/or output sequences to affect. 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

      sp 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().

      Many implementations do not update the get area inseekpos(), delegating tounderflow() that is called by the nextsgetc().

      [edit]Example

      On some implementations, the get area is emptied byseekpos() and the secondunderflow() is necessary to observe the effects.

      Run this code
      #include <fstream>#include <iostream> struct mybuf:std::filebuf{    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::filebuf::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";// uncomment if get area is emptied by seekpos()//        std::filebuf::underflow();//        std::cout << "after forced underflow(), size of the get area is "//                  << egptr() - eback() << " with "//                  << egptr() - gptr() << " read positions available.\n"; return rc;}}; int main(){    mybuf buf;    buf.open("test.txt",std::ios_base::in);std::istream stream(&buf);    stream.get();// read one char to force underflow()    stream.seekg(2);}

      Possible output:

      Before seekpos(2), size of the get area is 110 with 109 read positions available.seekpos() returns 2.After the call, size of the get area is 110 with 108 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
      LWG 171C++98the sequence of the operations of reposition was not clearmade clear

      [edit]See also

      invokesseekpos()
      (public member function ofstd::basic_streambuf<CharT,Traits>)[edit]
      [virtual]
      repositions the file position, using relative addressing
      (virtual protected member function)[edit]
      moves the file position indicator to a specific location in a file
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_filebuf/seekpos&oldid=143604"

      [8]ページ先頭

      ©2009-2025 Movatter.jp