public member function
<streambuf> <iostream>

std::basic_streambuf::snextc

int_type snextc();
Advance to next position and get character
Advances the current position of the controlled input sequence to the next character, and returns that next character.

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 first calls sbumpc, and if that function returns a valid character, the function then calls sgetc. This only calls virtual members if, at some point, there are no read positions available at the get pointer (gptr).

Its behavior is the same as if implemented as:
1
2
3
4
int_type snextc() {
  if ( sbumpc() == traits_type::eof() ) return traits_type::eof();
  else return sgetc();
}

Parameters

none

Return Value

The character at the next 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
17
// show file content - snextc() 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 the input 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).

Invalid arguments cause undefined behavior.

See also