stringstream

How to Use getline() and stringstream in C++

How to use getline() with stringstream in C++: read lines from a string, split text on a delimiter, and avoid the common mix-up of >> with getline().

On this page
  1. The getline syntax
  2. Reading lines from a multi-line string
  3. Splitting a line on a delimiter
  4. Mixing >> and getline
  5. Reading a line from std::cin and parsing it
  6. Common mistakes
  7. Summary

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. getline removes 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 getline keeps it. Remove it with if (!line.empty() && line.back() == '\r') line.pop_back();.
  • Mixing >> and getline. 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.

The complete guide to stringstream

stringstream in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Heap vs Stack Memory in C++

Learn how heap and stack memory differ in C++: how each is allocated, how long data lives, the speed and size trade-offs, and common mistakes, with examples.

Heap vs Stack 5 min read

Is C++ Better Than C?

C++ grew out of C and adds classes, templates, and a large standard library. Neither is better in every case. See how they differ and which suits your goals.

C++ vs C 4 min read