std::getline reads text from a stream into a std::string, one line at a time by default. It works with any input stream, including an std::istringstream. Pair the two and you can process a block of text line by line, or split a line on any character you choose.
The getline syntax
std::getline(stream, text); // reads up to a newline
std::getline(stream, text, ','); // reads up to a comma
The third argument is the delimiter. getline reads characters up to the delimiter, removes the delimiter from the stream, and does not store it in text. The default delimiter is the newline character.
getline returns the stream. When no more text can be read, the stream converts to false, so a while loop stops at the end of the input.
Reading lines from a multi-line string
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string text = "first line\nsecond line\nthird line";
std::istringstream lines(text);
std::string line;
int number = 1;
while (std::getline(lines, line)) {
std::cout << number << ": " << line << '\n';
++number;
}
}
The output is:
1: first line
2: second line
3: third line
The last line does not need a trailing newline. getline still returns it, and the next call fails.
Splitting a line on a delimiter
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main() {
std::string row = "red,green,,blue";
std::istringstream fields(row);
std::vector<std::string> parts;
std::string part;
while (std::getline(fields, part, ',')) {
parts.push_back(part);
}
for (const std::string& p : parts) {
std::cout << "[" << p << "]\n";
}
}
The output is:
[red]
[green]
[]
[blue]
Empty fields are kept. The two commas in a row produce an empty string, which is what you want for data with missing values, but it can surprise you if you expect it to be skipped.
Mixing >> and getline
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::istringstream in("3\nhello world\n");
int count = 0;
in >> count; // reads 3 and leaves the newline behind
std::string line;
std::getline(in, line); // reads the empty rest of the first line
std::cout << "[" << line << "]\n";
std::getline(in, line); // reads "hello world"
std::cout << "[" << line << "]\n";
}
The output is:
[]
[hello world]
>> stops right after the number, so the newline is still waiting in the stream. The first getline takes that empty rest of the line. To avoid the problem, read every line with getline and parse the numbers from the line with a separate std::istringstream.
Reading a line from std::cin and parsing it
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string line;
std::getline(std::cin, line);
std::istringstream words(line);
std::string word;
int count = 0;
while (words >> word) {
++count;
}
std::cout << "Words: " << count << '\n';
}
A common pattern is to read a whole line from std::cin with getline, then parse that line with a stringstream. Because the line is read first, the newline trap from the previous section does not happen.
Common mistakes
- Expecting the delimiter in the result.
getlineremoves it. Add it back yourself if you need it. - Windows line endings. A file saved with Windows line endings ends each line with a carriage return character, and
getlinekeeps it. Remove it withif (!line.empty() && line.back() == '\r') line.pop_back();. - Mixing
>>andgetline. See the section above for the fix. - Assuming empty fields are skipped. Each delimiter ends a field, even when the field is empty.
Summary
std::getline with std::istringstream lets you walk through text line by line or split it on any delimiter. Read whole lines first when you can, and the parsing stays predictable.