Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_istream<CharT,Traits>::seekg

      From cppreference.com
      <cpp‎ |io‎ |basic istream
       
       
       
       
      basic_istream& seekg( pos_type pos);
      (1)
      basic_istream& seekg( off_type off,std::ios_base::seekdir dir);
      (2)

      Sets input position indicator of the current associatedstreambuf object.

      Before doing anything else,seekg clearseofbit.(since C++11)

      seekg behaves asUnformattedInputFunction, except thatgcount() is not affected. After constructing and checking the sentry object,

      1) iffail()!=true, sets the input position indicator to absolute (relative to the beginning of the file) valuepos. Specifically, executesrdbuf()->pubseekpos(pos,std::ios_base::in) (pubseekpos, in turn, calls seekpos of the specific buffer, such asbasic_filebuf::seekpos,basic_stringbuf::seekpos, orstrstreambuf::seekpos). In case of failure, callssetstate(std::ios_base::failbit).
      2) iffail()!=true, sets the input position indicator to positionoff, relative to position, defined bydir. Specifically, executesrdbuf()->pubseekoff(off, dir,std::ios_base::in). In case of failure, callssetstate(std::ios_base::failbit).

      Contents

      [edit]Parameters

      pos - absolute position to set the input position indicator to
      off - relative position (positive or negative) to set the input 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

      [edit]
      failure if an error occurred (the error state flag is notgoodbit) andexceptions() is set to throw for that state.

      If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.

      [edit]Notes

      seekg(n) is not necessarily equivalent toseekg(n, ios::beg).std::basic_ifstream, for example, requires the absolute positionn to come fromtellg().

      [edit]Example

      Run this code
      #include <iostream>#include <sstream>#include <string> int main(){std::string str="Hello, world";std::istringstream in(str);std::string word1, word2;     in>> word1;    in.seekg(0);// rewind    in>> word2; std::cout<<"word1 = "<< word1<<'\n'<<"word2 = "<< word2<<'\n';}

      Output:

      word1 = Hello,word2 = Hello,

      [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++98seekg could set the output streamonly sets the input stream
      LWG 537C++98the type ofoff wasoff_type&corrected tooff_type

      [edit]See also

      returns the input position indicator
      (public member function)[edit]
      returns the output position indicator
      (public member function ofstd::basic_ostream<CharT,Traits>)[edit]
      sets the output position indicator
      (public member function ofstd::basic_ostream<CharT,Traits>)[edit]
      invokesseekpos()
      (public member function ofstd::basic_streambuf<CharT,Traits>)[edit]
      [virtual]
      repositions the file position, using absolute addressing
      (virtual protected member function ofstd::basic_filebuf<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 next pointer in the input sequence, output sequence, or both using absolute addressing
      (virtual protected member function ofstd::strstreambuf)[edit]
      invokesseekoff()
      (public member function ofstd::basic_streambuf<CharT,Traits>)[edit]
      [virtual]
      repositions the file position, using relative addressing
      (virtual protected member function ofstd::basic_filebuf<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 next pointer in the input sequence, output sequence, or both, using relative addressing
      (virtual protected member function ofstd::strstreambuf)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_istream/seekg&oldid=158534"

      [8]ページ先頭

      ©2009-2025 Movatter.jp