- istream[meta header]
- std[meta namespace]
- basic_istream[meta class]
- function[meta id-type]
pos_type tellg();(非書式化入力関数)ストリームバッファから現在の読み取り位置を取得する。
非書式化入力関数であるが、後続のgcount()呼び出しに影響を及ぼさない点が通常と異なる。
sentryオブジェクトを構築する。- 成功した場合、
rdbuf()->pubseekoff(0, cur, in)を呼び出して戻り値とする。
fail() == falseであれば、rdbuf()->pubseekoff(0, cur, in)。fail() == trueであれば、pos_type(-1)。
#include <iostream>
#include <sstream>
int main() {
std::istringstream is("103 201");
int x;
is >> x;
std::cout << x << std::endl;
auto pos = is.tellg(); // 現在位置をposに保存。
is >> x;
std::cout << x << std::endl;
is.seekg(pos); // 保存した位置を復元。
is >> x;
std::cout << x << std::endl;
}- tellg()[color ff0000]
- seekg[link seekg.md]
103
201
201
pos_type tellg(pos_type pos) {
try {
sentry s(*this, true);
if (s) {
return this->rdbuf()->pubseekoff(0, cur, ios_base::in);
}
} catch (...) {
例外を投げずにbadbitを設定する;
if ((this->exceptions() & badbit) != 0) {
throw;
}
}
return pos_type(-1);
}- sentry[link sentry.md]
- rdbuf()[link /reference/ios/basic_ios/rdbuf.md]
- pubseekoff[link /reference/streambuf/basic_streambuf/pubseekoff.md]
- exceptions()[link /reference/ios/basic_ios/exceptions.md]
- badbit[link /reference/ios/ios_base/type-iostate.md]
- C++98
basic_istream::seekgbasic_streambuf::pubseekoffbasic_streambuf::seekoff- P1264R2 Revising the wording of stream input operations
- C++23でローカルエラー状態の概念が導入され、入力関数のエラー処理セマンティクスが明確化された