public member function
<streambuf> <iostream>

std::streambuf::sgetc

int sgetc();
Get current character
Returns the character at the current position of the controlled input sequence, without modifying the current position.

Notice that, although similar, the following member functions have different behaviors:
member functiondescription
sgetc()returns the character at the current position.
sbumpc()returns the character at the current position and advances the current position to the next character.
snextc()advances the current position to the next character and returns this next character.

Internally, the function calls the virtual protected member underflow if there are no read positions available at the get pointer (gptr). Otherwise, the function uses the get pointer (gptr) directly, without calling virtual member functions.

Its behavior is the same as if implemented as:
1
2
3
4
int sgetc() {
  if ( (!gptr()) || (gptr()==egptr()) ) return underflow();
  else return *gptr();
}

Parameters

none

Return Value

The character at the current position of the controlled input sequence, as a value of type int.
If there are no more characters to read from the controlled input sequence, the function returns the end-of-file value (EOF).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// show file content - sgetc() example
#include <iostream>     // std::cout, std::streambuf
#include <fstream>      // std::ifstream
#include <cstdio>       // EOF

int main () {
  std::ifstream istr ("test.txt");
  if (istr) {
    std::streambuf * pbuf = istr.rdbuf();
    do {
      char ch = pbuf->sgetc();
      std::cout << ch;
    } while ( pbuf->snextc() != EOF );
    istr.close();
  }
  return 0;
}

This example shows the content of a file on screen, using the combination of sgetc and snextc to read a file.

Data races

Modifies 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