Skip to content

Files

Latest commit

3d75aee · Feb 4, 2024

History

History
88 lines (72 loc) · 1.98 KB

tellg.md

File metadata and controls

88 lines (72 loc) · 1.98 KB

tellg

  • istream[meta header]
  • std[meta namespace]
  • basic_istream[meta class]
  • function[meta id-type]
pos_type tellg();

概要

(非書式化入力関数)ストリームバッファから現在の読み取り位置を取得する。

非書式化入力関数であるが、後続のgcount()呼び出しに影響を及ぼさない点が通常と異なる。

効果

  1. sentryオブジェクトを構築する。
  2. 成功した場合、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

参照