Skip to content

Latest commit

 

History

History
96 lines (79 loc) · 2.84 KB

seekp.md

File metadata and controls

96 lines (79 loc) · 2.84 KB

seekp

  • ostream[meta header]
  • std[meta namespace]
  • basic_ostream[meta class]
  • function[meta id-type]
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;
}
  • std::ostringstream[link ../../sstream/basic_ostringstream.md]
  • seekp[color ff0000]
  • str()[link ../../sstream/basic_ostringstream/str.md]

出力

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;
}
  • sentry[link sentry.md]
  • fail[link ../../ios/basic_ios/fail.md]
  • rdbuf[link ../../ios/basic_ios/rdbuf.md]
  • pubseekpos[link ../../streambuf/basic_streambuf/pubseekpos.md]
  • pubseekoff[link ../../streambuf/basic_streambuf/pubseekoff.md]
  • setstate[link ../../ios/basic_ios/setstate.md]

バージョン

言語

  • C++98

参照