std::basic_istream::get(3)
β¨ NAME β¨
hey bestie π welcome to the std::basic_istream::get(3) page from the C++ Standard Libary! this is literally all about std::basic_istream::get β the unformatted-input girlies that pull characters out of a stream. no cap, she's iconic.
πΈ Synopsis πΈ
okay so here are the overloads, bestie β keep these exact:
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:
- Reads one character and returns it if available. Otherwise, returns
Traits::eof()and setsfailbitandeofbit. - Reads one character and stores it to
chif available. Otherwise, leaveschunmodified and setsfailbitandeofbit. Note that this function is not overloaded on the typessigned charandunsigned char, unlike the formatted character inputoperator>>. - Same as
get(s, count, widen('\n')), that is, reads at moststd::max(0, count - 1)characters and stores them into character string pointed to bysuntil'\n'is found. - Reads characters and stores them into the successive locations of the character array whose first element is pointed to by
s. Characters are extracted and stored until any of the following occurs:countis less than1orcount - 1characters have been stored.- end of file condition occurs in the input sequence (
setstate(eofbit)is called). - the next available input character
cequalsdelim, as determined byTraits::eq(c, delim). This character is not extracted (unlikegetline()). In any case, ifcount > 0, a null character (CharT()) is stored in the next successive location of the array.
- Same as
get(strbuf, widen('\n')), that is, reads available characters and inserts them to the givenbasic_streambufobject until'\n'is found. - Reads characters and inserts them to the output sequence controlled by the given
basic_streambufobject. Characters are extracted and inserted intostrbufuntil 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 byTraits::eq(c, delim). This character is not extracted. - an exception occurs (in which case the exception is caught and not rethrown).
If no characters were extracted, calls setstate(failbit).
All versions set the value of gcount() to the number of characters extracted.
π Parameters π
chβ reference to the character to write the result tosβ pointer to the character string to store the characters tocountβ size of character string pointed to bysdelimβ delimiting character to stop the extraction at. It is not extracted and not storedstrbufβ stream buffer to read the content to
π Return value π
- (1) The extracted character or
Traits::eof(). - (2-6)
*this
π Exceptions π
failure if an error occurred (the error state flag is not goodbit) and exceptions() is set to throw for that state.
If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown.
π Example π
// Run this code
#include <iostream>
#include <sstream>
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
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π«Ά
- LWG 370 β applied to
C++98. Behavior as published: the effect of overload (5) wasget(s, count, widen('\n')), which is the effect of overload (3). Correct behavior: corrected toget(strbuf, widen('\n')). - LWG 531 β applied to
C++98. Behavior as published: overloads (3,4) could not handle the case wherecountis non-positive. Correct behavior: no character is extracted in this case.
π₯ See also π₯
readβ extracts blocks of characters (public member function)operator>>β extracts formatted data (public member function)operator>>(std::basic_istream)β extracts characters and character arrays (function template)
and that's the tea, bestie π§π huge shoutout to http://cppreference.com, version/date 2024.06.10 β catch you on the next manpage! π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms