public member function
<streambuf> <iostream>

std::basic_streambuf::in_avail

streamsize in_avail();
Get number of character available to read
Returns the number of characters available to read. This value depends on whether there are read positions directly available at the get pointer (gptr):
  • If the get pointer (gptr) has a value and this is less than the end pointer (egptr): The function returns the number of characters directly available at the get pointer before the end pointer (i.e., it returns (egptr()-gptr()) without calling any virtual member function).
  • If the get pointer (gptr) is either null or has reached the end pointer (egptr): The function calls the protected virtual member function showmanyc to obtain the number of characters expected to be available after an underflow.

Parameters

none

Return Value

The number of characters available to read.
A value of -1 (obtained from showmanyc) represents that no further characters are expected to be available.
streamsize is a signed integral type.

Example

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;
}


Data races

This member function may modify the stream buffer object.
Concurrent access to the same stream buffer object may introduce data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream buffer is in a valid state (this also applies to standard derived classes).

See also