public member function
<streambuf> <iostream>

std::basic_streambuf::sgetc

int_type 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_type sgetc() {
  if ( (!gptr()) || (gptr()==egptr()) ) return underflow();
  else return traits_type::to_int_type(*gptr());
}

Parameters

none

Return Value

The character at the current position of the controlled input sequence, converted to a value of type int_type using member traits_type::to_int_type.
If there are no more characters to read from the controlled input sequence, the function returns the end-of-file value (traits_type::eof()).
Member type int_type is an integral type able to represent any character value or the special end-of-file value.

Example

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

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() != std::streambuf::traits_type::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