public member function
<streambuf> <iostream>

std::streambuf::in_avail

streamsize in_avail();
獲取可讀取的字元數
返回可讀取的字元數。此值取決於在get pointergptr)處是否有直接可用的讀取位置。
  • 如果get pointergptr)有值且小於end pointeregptr):函式返回get pointerend pointer之間的直接可用的字元數(即,它返回 (egptr()-gptr()),而不呼叫任何虛成員函式)。
  • 如果get pointergptr)為 null 或已到達end pointeregptr):函式呼叫受保護的虛成員函式 showmanyc 來獲取underflow後預期可用的字元數。

引數



返回值

可讀取的字元數。
值為 -1(來自 showmanyc)表示沒有預期會提供更多字元。
streamsize 是一個帶符號整型。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// get file buffer size using in_avail
#include <iostream>     // std::cout, std::streambuf, std::streamsize
#include <fstream>      // std::ifstream

int main () {
  std::ifstream ifs ("test.txt");
  if (ifs.good()) {
    std::streambuf* pbuf = ifs.rdbuf();
    char c; ifs >> c;
    std::streamsize size = pbuf->in_avail();
    std::cout << "first character in file: " << c << '\n';
    std::cout << size << " characters in buffer after it\n";
  }
  ifs.close();

  return 0;
}

資料競爭

此成員函式可能會修改stream buffer物件。
同時訪問同一*流緩衝區*物件可能會導致資料競爭。

異常安全

基本保證:如果丟擲異常,則流緩衝區處於有效狀態(這也適用於標準派生類)。

另見