std::basic_...eam::getline(3)
π std::basic_istream::getline π
okay bestie, gather round because we're talking about std::basic_istream::getline from the C++ Standard Library and honestly? it's giving "reads a whole line but make it customizable" energy. this is the manual page for the C++ Standard Library's getline member function, and yes you're gonna understand it by the end. no cap. π«Ά
β¨ Synopsis β¨
basic_istream& getline( char_type* s, std::streamsize count );
(1)
basic_istream& getline( char_type* s, std::streamsize count,
char_type delim );
(2)
so what does it do? it extracts characters from the stream until end of line or until the specified delimiter delim, whichever hits first. iconic behavior honestly. π
the first overload is literally equivalent to getline(s, count, widen('\n')). same thing, just with the newline as the default delimiter. slay.
it behaves as an UnformattedInputFunction. after constructing and checking the sentry object, it extracts characters from *this and stores them in successive locations of the array whose first element is pointed to by s, until any of the following occurs (and yes, they're tested in the order shown, this matters bestie):
- end of file condition occurs in the input sequence.
- the next available character
cis the delimiter, as determined byTraits::eq(c, delim). the delimiter is extracted (unlikebasic_istream::get()) and counted towardsgcount(), but is not stored. periodt. countis non-positive, orcount - 1characters have been extracted (in this casesetstate(failbit)is called).
if the function extracts no characters, failbit is set in the local error state before setstate() is called.
in any case, if count > 0, it then stores a null character CharT() into the next successive location of the array and updates gcount().
πΈ Notes πΈ
because condition #2 is tested before condition #3, the input line that exactly fits the buffer does not trigger failbit. that's the vibe check passing, bestie. β
because the terminating character is counted as an extracted character, an empty input line does not trigger failbit. so don't panic if you get a blank line, it's literally fine. π
π Parameters π
sβ 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 extracted but not stored.
π¦ Return value π¦
*this β that's it. it returns itself like a main character. πββοΈ
π₯ 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.
so basically, don't ignore your error states, bestie. they will come for you. π
β Example β
#include <array>
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
std::istringstream input("abc|def|gh");
std::vector<std::array<char, 4>> v;
// note: the following loop terminates when std::ios_base::operator bool()
// on the stream returned from getline() returns false
for (std::array<char, 4> a; input.getline(&a[0], 4, '|');)
v.push_back(a);
for (auto& a : v)
std::cout << &a[0] << '\n';
}
Output:
abc
def
gh
look at that. clean. iconic. no notes. π
π§ Defect reports π§
the following behavior-changing defect reports were applied retroactively to previously published C++ standards. yes bestie, they went back and fixed things. we love accountability. π«Ά
- LWG 531 β Applied to C++98 β Behavior as published:
std::getlinecould not handle the case wherecountis non-positive β Correct behavior: no character is extracted in this case
ngl that's a glow-up. β¨
π See also π
getlineβ read data from an I/O stream into a string (function template)operator>>β extracts formatted data (public member function)getβ extracts characters (public member function)readβ extracts blocks of characters (public member function)
and that's the whole page bestie, you ate that. π β¨
this little masterpiece comes from http://cppreference.com, dated 2024.06.10, and the page title is std::basic_...eam::getline(3). go forth and read your lines with confidence. ππ¦π
π¦ 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