The C++ std::istream::get() function is used to read characters from an input stream. It can operate in several modes: reading a single character, reaading character into a buffer until a specified delimiter is encountered, or reading characters into buffer of a specified size.
SyntaxThis function does not skip the whitespace by default, making it distinct from functions like operator>>.
Following is the syntax for std::istream::get() function.
int get();istream& get (char& c); or istream& get (char* s, streamsize n);istream& get (char* s, streamsize n, char delim); or istream& get (streambuf& sb);istream& get (streambuf& sb, char delim);Parameters
This function returns the character read, or the end-of-file value (traits_type::eof()) if no characters are available in the stream (note that in this case, the failbit flag is also set).
ExceptionsIf an exception is thrown, the object is in a valid state.
Data racesModifies c, sb or the elements in the array pointed by s and modifies the stream object.
ExampleIn the following example, we are going to read the single character.
#include <iostream> int main() { char x; std::cout << "Enter A Character : "; std::cin.get(x); std::cout << "Entered Character : " << x << std::endl; return 0; }Output
Output of the above code is as follows −
Enter A Character : B Entered Character : BExample
Consider the following example, where we are going to use the get() to read characters from input stream until a delimiter is encounterd.
#include <iostream> int main() { char a[10]; std::cout << "Enter Input : "; std::cin.get(a, 10, ','); std::cout << "Entered Input : " << a << std::endl; return 0; }Output
Following is the output of the above code −
Enter Input : Hello, Namaste! Entered Input : Hello
istream.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4