std::basic_istream<CharT,Traits>::get
| int_type get(); |
(1) | |
| basic_istream& get( char_type& ch ); |
(2) | |
| basic_istream& get( char_type* s, std::streamsize count ); |
(3) | |
| basic_istream& get( char_type* s, std::streamsize count, char_type delim ); |
(4) | |
| basic_istream& get( basic_streambuf& strbuf ); |
(5) | |
| basic_istream& get( basic_streambuf& strbuf, char_type delim ); |
(6) | |
Extracts character or characters from stream.
All versions behave as UnformattedInputFunctions. After constructing and checking the sentry object, these functions perform the following:
ch if available. Otherwise, leaves ch unmodified and sets failbit and eofbit. Note that this function is not overloaded on the types signed char and unsigned char, unlike the formatted character input operator>>.s until '\n' is found.s. Characters are extracted and stored until any of the following occurs:- count-1 characters have been stored
- end of file condition occurs in the input sequence (setstate(eofbit) is called)
- the next available input character
cequalsdelim, as determined by Traits::eq(c, delim). This character is not extracted (unlike basic_istream::getline())
- the next available input character
count>0, a null character (CharT() is stored in the next successive location of the array.strbuf until any of the following occurs:- end of file condition occurs in the input sequence
- inserting into the output sequence fails (in which case the character that could not be inserted, is not extracted)
- the next available input character
cequalsdelim, as determined by Traits::eq(c, delim). This character is not extracted.
- the next available input character
- an exception occurs (in which case the exception is caught and not rethrown)
All versions set the value of gcount() to the number of characters extracted.
Contents |
[edit] Parameters
| ch | - | reference to the character to write the result to |
| s | - | pointer to the character string to store the characters to |
| count | - | size of character string pointed to by s
|
| delim | - | delimiting character to stop the extraction at. It is not extracted and not stored. |
| strbuf | - | stream buffer to read the content to |
[edit] Return value
[edit] Exceptions
If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown.
[edit] Example
#include <sstream> #include <iostream> int main() { std::istringstream s1("Hello, world."); char c1 = s1.get(); // reads 'H' std::cout << "after reading " << c1 << ", gcount() == " << s1.gcount() << '\n'; char c2; s1.get(c2); // reads 'e' char str[5]; s1.get(str, 5); // reads "llo," std::cout << "after reading " << str << ", gcount() == " << s1.gcount() << '\n'; std::cout << c1 << c2 << str; s1.get(*std::cout.rdbuf()); // reads the rest, not including '\n' std::cout << "\nAfter the last get(), gcount() == " << s1.gcount() << '\n'; }
Output:
after reading H, gcount() == 1 after reading llo,, gcount() == 4 Hello, world. After the last get(), gcount() == 7
[edit] See also
| extracts blocks of characters (public member function) | |
| extracts formatted data (public member function) | |
| extracts characters and character arrays (function template) |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
