- 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]
- std::istringstream[link /reference/sstream/basic_istringstream.md]
- 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