std::basic_istream<CharT,Traits>::getline
Min standard notice:
Extracts characters from stream until end of line or the specified delimiter delim.
# Declarations
basic_istream& getline( char_type* s, std::streamsize count );
basic_istream& getline( char_type* s, std::streamsize count, char_type delim );
# Parameters
s: pointer to the character string to store the characters tocount: size of character string pointed to by sdelim: delimiting character to stop the extraction at. It is extracted but not stored.
# Return value
*this
# Notes
Because condition #2 is tested before condition #3, the input line that exactly fits the buffer does not trigger failbit.
Because the terminating character is counted as an extracted character, an empty input line does not trigger failbit.
# 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';
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 531 | C++98 | std::getline could not handle thecase where count is non-positive | no character isextracted in this case |