Skip to content

Latest commit

 

History

History
127 lines (108 loc) · 3.52 KB

seekg.md

File metadata and controls

127 lines (108 loc) · 3.52 KB

seekg

  • istream[meta header]
  • std[meta namespace]
  • basic_istream[meta class]
  • function[meta id-type]
basic_istream<CharT, Traits>& seekg(pos_type pos);
basic_istream<CharT, Traits>& seekg(off_type off, seekdir dir);

概要

(非書式化入力関数)ストリームバッファに対し、読み取り位置の移動を指示する。

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

seekgは、seek getの略称。「読み取り用の位置の移動」を意味する。

効果

  1. pos_typeを引数に取るもののみ)初めにeofbitを消去する。
  2. sentryオブジェクトを構築する。sentryオブジェクトが失敗を示した場合、何もしない。
  3. 与えられた実引数により、以下のいずれかを実行する。
    • rdbuf()->pubseekpos(pos, ios_base::in)
    • rdbuf()->pubseekoff(off, dir, ios_base::in)
  4. 失敗した場合、setstate(failbit)を呼び出す。

戻り値

*this

以下は、off_typeseekdirを使用する例。 pos_typeのみを引数に取るオーバーロードの例は、tellgを参照。

#include <iostream>
#include <sstream>

int main() {
  std::istringstream is("ABC");
  char x;

  is >> x;
  std::cout << x << std::endl;

  is.seekg(0, std::ios_base::beg);
  is >> x;
  std::cout << x << std::endl;
}
  • seekg[color ff0000]
  • std::istringstream[link /reference/sstream/basic_istringstream.md]
  • std::ios_base[link /reference/ios/ios_base.md]
  • beg[link /reference/ios/ios_base/type-seekdir.md]

出力

A
A

実装例

basic_istream<CharT, Traits>& seekg(pos_type pos) {
  iostate state = goodbit;
  try {
    this->clear(this->rdstate() & ~eofbit);
    sentry s(*this, true);
    if (s) {
      if (this->rdbuf()->pubseekpos(pos, ios_base::in) == -1) {
        state |= failbit;
      }
    }
  } catch (...) {
    例外を投げずにbadbitを設定する;
    if ((this->exceptions() & badbit) != 0) {
      throw;
    }
  }
  this->setstate(state);
  return *this;
}

basic_istream<CharT, Traits>& seekg(off_type off, seekdir dir) {
  iostate state = goodbit;
  try {
    sentry s(*this, true);
    if (s) {
      if (this->rdbuf()->pubseekoff(off, dir, ios_base::in) == -1) {
        state |= failbit;
      }
    }
  } catch (...) {
    例外を投げずにbadbitを設定する;
    if ((this->exceptions() & badbit) != 0) {
      throw;
    }
  }
  this->setstate(state);
  return *this;
}
  • clear[link /reference/ios/basic_ios/clear.md]
  • rdstate()[link /reference/ios/basic_ios/rdstate.md]
  • eofbit[link /reference/ios/ios_base/type-iostate.md]
  • sentry[link sentry.md]
  • rdbuf()[link /reference/ios/basic_ios/rdbuf.md]
  • pubseekpos[link /reference/streambuf/basic_streambuf/pubseekpos.md]
  • ios_base[link /reference/ios/ios_base.md]
  • in[link /reference/ios/ios_base/type-openmode.md]
  • failbit[link /reference/ios/ios_base/type-iostate.md]
  • badbit[link /reference/ios/ios_base/type-iostate.md]
  • exceptions()[link /reference/ios/basic_ios/exceptions.md]
  • setstate[link /reference/ios/basic_ios/setstate.md]

バージョン

言語

  • C++98

参照