CKTIS / C++
std::getline: read text up to a delimiter
std::getline replaces a std::string with stream characters up to a delimiter that is extracted but not appended.
Version: C++23
Syntax
template<class CharT, class Traits, class Allocator>
std::basic_istream<CharT, Traits>&
std::getline(std::basic_istream<CharT, Traits>& input,
std::basic_string<CharT, Traits, Allocator>& text,
CharT delimiter);
// Without delimiter, uses input.widen('\n').std::getline(input, text, delim) behaves as an unformatted input function: after a successful sentry it calls text.erase(), then appends characters until EOF, the next delim, or text.max_size(). The delimiter is extracted from the stream but not stored. The overload without a delimiter uses input.widen('\n') and does not affect a later gcount() result.
The example reads red,green\n: the first call stops at the comma and the second at the newline, so it prints red|green. The destination is replaced rather than extended from its previous contents.
If no character is extracted, failbit is set; EOF sets eofbit, and reaching max_size() also sets failbit. Bits enabled in exceptions() can cause std::ios_base::failure. After formatted operator>>, a leftover newline can produce an empty record; consume it with ignore or deliberately use std::ws, noting that std::ws removes all leading whitespace.
Examples
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream input{"red,green\n"};
std::string first;
std::string second;
std::getline(input, first, ',');
std::getline(input, second);
std::cout << first << '|' << second << '\n';
}
Result
red|green
std::getline → red|green