Namespaces
Variants

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

From cppreference.net
basic_istream & seekg ( pos_type pos ) ;
(1)
basic_istream & seekg ( off_type off, std:: ios_base :: seekdir dir ) ;
(2)

現在関連付けられている streambuf オブジェクトの入力位置インジケータを設定します。

他の処理を行う前に、 seekg eofbit をクリアします。 (C++11以降)

seekg UnformattedInputFunction として動作しますが、 gcount() には影響しません。セントリオブジェクトを構築してチェックした後、

1) if fail ( ) ! = true の場合、入力位置指示子を絶対値(ファイルの先頭からの相対値) pos に設定します。具体的には、 rdbuf ( ) - > pubseekpos ( pos, std:: ios_base :: in ) を実行します( pubseekpos は、次に特定のバッファ( basic_filebuf::seekpos basic_stringbuf::seekpos 、または strstreambuf::seekpos )のseekposを呼び出します)。失敗した場合、 setstate ( std:: ios_base :: failbit ) を呼び出します。
2) fail() != true の場合、入力位置指示子を dir で定義された位置からの相対位置 off に設定する。具体的には rdbuf()->pubseekoff(off, dir, std::ios_base::in) を実行する。失敗した場合、 setstate(std::ios_base::failbit) を呼び出す。

目次

パラメータ

pos - 入力位置指示子を設定する絶対位置
off - 入力位置指示子を設定する相対位置(正または負)
dir - 相対オフセットを適用する基準位置を定義します。以下の定数のいずれかになります:
定数 説明
beg ストリームの先頭
end ストリームの末尾
cur ストリーム位置指示子の現在位置

戻り値

* this

例外

failure **翻訳結果:** failure **説明:** - HTMLタグ (` `, ` `) と属性 (`class`, `href`, `title`) は翻訳せず保持 - `failure` はC++の標準ライブラリクラス名であるため、専門用語として翻訳せず保持 - 元のフォーマットと構造を完全に維持 if an error occurred (the error state flag is not goodbit ) and exceptions() is set to throw for that state.

内部操作が例外をスローした場合、それは捕捉され、 badbit が設定されます。 exceptions() badbit に対して設定されている場合、例外は再スローされます。

注記

seekg(n) は必ずしも seekg(n, ios::beg) と等価ではありません。 std:: basic_ifstream の場合、例えば、絶対位置 n tellg() から取得する必要があります。

#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';
}

出力:

word1 = Hello,
word2 = Hello,

不具合報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 129 C++98 失敗を示す方法がなかった 失敗時に failbit を設定
LWG 136 C++98 seekg が出力ストリームを設定する可能性があった 入力ストリームのみを設定
LWG 537 C++98 off の型が off_type& だった off_type に修正

関連項目

入力位置指示子を返す
(public member function)
出力位置指示子を返す
( std::basic_ostream<CharT,Traits> のpublic member function)
出力位置指示子を設定する
( std::basic_ostream<CharT,Traits> のpublic member function)
seekpos ( ) を呼び出す
( std::basic_streambuf<CharT,Traits> のpublic member function)
[virtual]
絶対アドレッシングを使用してファイル位置を再配置する
( std::basic_filebuf<CharT,Traits> のvirtual protected member function)
[virtual]
絶対アドレッシングを使用して入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
( std::basic_stringbuf<CharT,Traits,Allocator> のvirtual protected member function)
[virtual]
絶対アドレッシングを使用して入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
( std::strstreambuf のvirtual protected member function)
seekoff ( ) を呼び出す
( std::basic_streambuf<CharT,Traits> のpublic member function)
[virtual]
相対アドレッシングを使用してファイル位置を再配置する
( std::basic_filebuf<CharT,Traits> のvirtual protected member function)
[virtual]
相対アドレッシングを使用して入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
( std::basic_stringbuf<CharT,Traits,Allocator> のvirtual protected member function)
[virtual]
相対アドレッシングを使用して入力シーケンス、出力シーケンス、または両方の次のポインタを再配置する
( std::strstreambuf のvirtual protected member function)