C++ streambuf::snextc() function



The C++ std::stringbuf::snextc() function is used to advance the input sequence by one character and returns the new character at the current position. If the read operation is successful it return the next character otherwise, it return the traits_type::eof(), indicating the end of the stream or an error.

Syntax

Following is the syntax for std::streambuf::snextc() function.

int_type snextc();

Parameters

It does not accept any parameter.

Return Value

It returns 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.

Exceptions

If an exception is thrown, the stream buffer is in a valid state.

Data races

It modifies the stream buffer object.

Example 1

In the following example, we are going to consider the basic usage of the snextc() function.

#include <iostream>
#include <sstream>
int main() {
   std::istringstream a("Vanakam");
   std::streambuf * b = a.rdbuf();
   char c = b -> snextc();
   std::cout << "Result : " << c << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result : a

Example 2

Consider the following example, where we are going to loop through each character in the input sequence.

#include <iostream>
#include <sstream>
int main() {
   std::istringstream a("ATUTORIALSPOINT");
   std::streambuf * v = a.rdbuf();
   char c;
   while ((c = v -> snextc()) != std::char_traits < char > ::eof()) {
      std::cout << c << " ";
   }
   std::cout << std::endl;
   return 0;
}

Output

Following is the output of the above code −

T U T O R I A L S P O I N T 
streambuf.htm
Advertisements