public member function
<istream> <iostream>

std::basic_istream::tellg

pos_type tellg();
獲取輸入序列中的位置
返回輸入流中當前字元的位置。

內部,該函式透過首先構造一個 sentry 物件(將 noskipws 設定為 true)而不對其進行求值來訪問輸入序列。然後,如果成員 fail 返回 true,則該函式返回 -1
否則,返回 rdbuf()->pubseekoff(0,cur,in)。最後,在返回前銷燬 sentry 物件。

請注意,即使在呼叫之前設定了 eofbit 標誌,該函式也能正常工作。

呼叫此函式不會改變 gcount 返回的值。

引數



返回值

流中的當前位置。
如果與流關聯的 *流緩衝區* 不支援該操作,或者操作失敗,則函式返回 -1
成員型別 pos_type 由 *字元特性* 決定:通常,它是一個 fpos 型別(例如 streampos),可以與整數型別相互轉換。

錯誤透過修改 *內部狀態標誌* 來指示
標誌錯誤
eofbit-
failbit構造 sentry 失敗(例如,當呼叫前的 *流狀態* 不是 good 時)。
badbit流錯誤(例如,當此函式捕獲由內部操作丟擲的異常時)。
設定後,流的完整性可能受到影響。
一次操作可以設定多個標誌。

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

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// read a file into memory
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {
  std::ifstream is ("test.txt", std::ifstream::binary);
  if (is) {
    // get length of file:
    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    // allocate memory:
    char * buffer = new char [length];

    // read data as a block:
    is.read (buffer,length);

    is.close();

    // print content:
    std::cout.write (buffer,length);

    delete[] buffer;
  }

  return 0;
}

在此示例中,tellg 用於獲取流在被 seekg 移動到流末尾後的位置,從而確定檔案大小。

資料競爭

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

異常安全

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

另見