std::basic_istream<CharT,Traits>:: seekg
|
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()
には影響しません。セントリオブジェクトを構築してチェックした後、
fail() != true
の場合、入力位置指示子を
dir
で定義された位置からの相対位置
off
に設定する。具体的には
rdbuf()->pubseekoff(off, dir, std::ios_base::in)
を実行する。失敗した場合、
setstate(std::ios_base::failbit)
を呼び出す。
目次 |
パラメータ
| pos | - | 入力位置指示子を設定する絶対位置 | ||||||||
| off | - | 入力位置指示子を設定する相対位置(正または負) | ||||||||
| dir | - |
相対オフセットを適用する基準位置を定義します。以下の定数のいずれかになります:
|
戻り値
* this
例外
内部操作が例外をスローした場合、それは捕捉され、
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)
|