public member function
<ostream> <iostream>

std::basic_ostream::seekp

(1)
basic_ostream& seekp (pos_type pos);
(2)
basic_ostream& seekp (off_type off, ios_base::seekdir way);
設定輸出序列中的位置
設定下一個字元將被插入到輸出流中的位置。

內部地,如果成員 fail 返回 true,則函式返回。
否則,它呼叫其關聯的 *流緩衝區* 物件(如果存在)上的 pubseekpos *(1)pubseekoff *(2)
內部地,函式首先構造一個不評估的 sentry 物件來訪問輸出序列。然後,如果成員 fail 返回 true,則函式返回。
否則,它呼叫其關聯的 *流緩衝區* 物件(如果存在)上的 pubseekpos *(1)pubseekoff *(2)
最後,它在返回之前銷燬 sentry 物件。

請注意,即使 eofbit 標誌在呼叫前已被設定,該函式也能正常工作,但它不會修改該標誌。

引數

pos
在流內的新的絕對位置(相對於開頭)。
*字元特性* 確定成員型別 pos_type:通常,它是一個 fpos 型別(例如 streampos),可以與整數型別相互轉換。
off
偏移量,相對於 way 引數。
成員型別 off_type 由 *字元特性* 確定:通常,它是已簽名整型 streamoff 的別名。
way
型別為 ios_base::seekdir 的物件。它可以接受以下任何常量值
偏移量相對於...
ios_base::beg流的開頭
ios_base::cur流中的當前位置
ios_base::end流的末尾

返回值

basic_ostream 物件(*this)。

錯誤透過修改 *內部狀態標誌* 來指示
flagerror
eofbit-
failbit內部呼叫 *(1) 中的 pubseekpos 失敗(當 sentry 的構造失敗時也可能設定該標誌)。
badbit流上發生了其他錯誤(例如,當函式捕獲由內部操作丟擲的異常時)。
當設定此標誌時,流的完整性可能受到影響。
flagerror
eofbit-
failbit內部呼叫 pubseekpospubseekoff 失敗(當 sentry 的構造失敗時也可能設定該標誌)。
badbit流上發生了其他錯誤(例如,當函式捕獲由內部操作丟擲的異常時)。
當設定此標誌時,流的完整性可能受到影響。
一次操作可能會設定多個標誌。

如果操作設定了一個已透過成員 exceptions 註冊的 *內部狀態標誌*,則函式將丟擲成員型別為 failure 的異常。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// position in output stream
#include <fstream>      // std::ofstream

int main () {

  std::ofstream outfile;
  outfile.open ("test.txt");

  outfile.write ("This is an apple",16);
  long pos = outfile.tellp();
  outfile.seekp (pos-7);
  outfile.write (" sam",4);

  outfile.close();

  return 0;
}

在此示例中,tellp 用於獲取寫操作後流中的位置。然後,指標向前移動 7 個字元以在該位置修改檔案,因此檔案的最終內容為
This is a sample


資料競爭

修改流物件。
併發訪問同一個流物件可能導致資料爭用。

異常安全

基本保證:如果丟擲異常,物件處於有效狀態。
如果生成的 *錯誤狀態標誌* 不是 goodbit 並且成員 exceptions 被設定為針對該狀態丟擲,則它會丟擲成員型別為 failure 的異常。
由內部操作引起的任何異常都會被函式捕獲並處理,設定 badbit。如果在上一次呼叫 exceptions 時設定了 badbit,則函式會重新丟擲捕獲到的異常。

另見