• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <ostream>

    std::basic_ostream::seekp

    basic_ostream<CharT, Traits>& seekp(pos_type pos);              // (1)
    basic_ostream<CharT, Traits>& seekp(off_type off, seekdir dir); // (2)
    

    概要

    ストリームバッファに対し、書き込み位置の移動を指示する。

    seekpは、seek putの略称。「書き込み用の位置の移動」を意味する。

    効果

    • (1) 出力ストリームの書き込み位置を pos に設定する。
    • (2) 出力ストリームの書き込み位置を dir を基準として相対位置 off に設定する。

    戻り値

    *this

    備考

    本関数の処理内容は以下の通り。

    1. sentry オブジェクトを構築する(C++11 以降のみ)。
    2. 与えられた実引数により、以下のいずれかを実行する。
    3. 処理に失敗した場合(上記の戻り値-1 だった場合)、setstate(failbit)を呼び出す。

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

    #include <iostream>
    #include <sstream>
    
    int main() {
      std::ostringstream os;
      os << "12345";
      os.seekp(-2, std::ios_base::cur);
      os << "ABC";
      std::cout << os.str() << std::endl;
    }
    

    出力

    123ABC
    

    実装例

    basic_ostream<CharT, Traits>& seekp(pos_type pos) {
      sentry s(*this);
      if (!this->fail()) {
        if (this->rdbuf()->pubseekpos(pos, ios_base::out) == pos_type(-1)) {
          this->setstate(failbit);
        }
      }
      return *this;
    }
    
    basic_ostream<CharT, Traits>& seekp(off_type off, seekdir dir) {
      sentry s(*this);
      if (!this->fail()) {
        if (this->rdbuf()->pubseekoff(off, dir, ios_base::out) == pos_type(-1)) {
          this->setstate(failbit);
        }
      }
      return *this;
    }
    

    バージョン

    言語

    • C++98

    参照