public member function
<streambuf> <iostream>

std::streambuf::snextc

int 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 snextc() {
  if ( sbumpc() == EOF ) return EOF;
  else return sgetc();
}


Parameters

none

Return Value

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