public member function
<streambuf> <iostream>

std::basic_streambuf::in_avail

streamsize in_avail();
獲取可讀字元數
返回可讀字元數。此值取決於是否有可用的讀取位置,這由*讀取指標*(gptr)決定。
  • 如果*讀取指標*(gptr)有值且小於*結束指標*(egptr):該函式返回*讀取指標*到*結束指標*之間直接可用的字元數(即,它返回 (egptr()-gptr()),而無需呼叫任何虛成員函式)。
  • 如果*讀取指標*(gptr)為 null 或已達到*結束指標*(egptr):該函式呼叫受保護的虛成員函式 showmanyc 來獲取*下溢*(underflow)後預期的可用字元數。

引數



返回值

可讀字元數。
值為-1(由showmanyc獲得)表示不再預期有任何字元可用。
streamsize 是一個帶符號整型。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// get file size using pubseekoff
#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;
}

資料競爭

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

異常安全

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

另見