C++ std::istream::getline() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, getline() reads a whole line of text from a stream, spaces included. It stops when it finds the new line character, removes it from the stream, and does not store it.

There are two versions. std::getline(stream, string) fills a std::string and is the one beginners should use. stream.getline(buffer, size) fills a character array. Include <iostream> and <string> to use them.

This is a member function of std::istream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Use the std::string version for most programs. Use the third argument to change the stopping character.

C++
std::string line;
std::getline(std::cin, line);        // read until '\n'
std::getline(in, field, ',');        // read until ','

char buf[50];
std::cin.getline(buf, 50);           // read into a char array
CallWhat it does
std::getline(std::cin, line)Reads one line from the keyboard into the string line.
std::getline(in, s, ',')Reads up to the next comma instead of the new line.
std::cin.getline(buf, 50)Reads at most 49 characters into buf and adds a .

If the line is longer than the buffer, the array version sets failbit. The string version has no such limit.

Library declarations
C++
// <string> free function
istream& getline(istream& is, string& str, char delim = '\n');

// member function of istream
istream& getline(char* s, streamsize count, char delim = '\n');

Parameters

ParameterDescription
isFree function only. The stream to read from, such as std::cin or a file stream.
strFree function only. The std::string that receives the line. Its old content is replaced.
sMember function only. A character array that receives the text.
countMember function only. The size of the array. At most count - 1 characters are stored.
delimOptional. The character that ends the read. The default is '\n'. The delimiter is removed but not stored.

Return Value

Return ValueDescription
istream&A reference to the stream. In a condition it is true when a line was read and false when nothing could be read.

C++ Examples

Example 1 — Split Text at Commas with getline()

The third argument changes the stopping character from a new line to a comma.

C++
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::istringstream in("red,green,blue");
    std::string color;

    while (std::getline(in, color, ',')) {
        std::cout << "color: " << color << '\n';
    }
    return 0;
}

How It Works

  1. std::getline(in, color, ',') reads up to the next comma and removes the comma from the stream.
  2. The last piece blue has no comma after it, but getline() still returns it when the text ends.
  3. When no characters are left, the call fails, the condition is false, and the loop ends.

Example 2 — getline() with a Character Array

The array version stops after size - 1 characters and sets failbit.

C++
#include <iostream>
#include <sstream>

int main() {
    std::istringstream in("abcdefghij\n");
    char buf[5];

    in.getline(buf, 5);

    std::cout << "buf = " << buf << '\n';
    std::cout << "fail: " << (in.fail() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. buf holds 5 characters, but one place is needed for the final \0. So only abcd fits.
  2. The line is longer than the space, so getline() stops early and sets failbit.
  3. This is why the std::string version is safer: a string grows to hold the whole line.

Example 3 — The >> then getline() Problem

Compare the wrong and the right way to read a number and then a name.

C++
#include <iostream>
#include <sstream>
#include <string>

int main() {
    int age = 0;
    std::string name;

    std::istringstream wrong("25\nMari Selvan\n");
    wrong >> age;
    std::getline(wrong, name);
    std::cout << "wrong: name = [" << name << "]\n";

    std::istringstream right("25\nMari Selvan\n");
    right >> age;
    right.ignore(100, '\n');
    std::getline(right, name);
    std::cout << "right: name = [" << name << "]\n";
    return 0;
}

How It Works

  1. wrong >> age reads 25 and stops before the new line. That new line is still in the stream.
  2. The first getline() sees the leftover new line right away and returns an empty string.
  3. In the right version, ignore(100, '\n') throws away the rest of the first line, so getline() reads Mari Selvan.

Frequently Asked Questions

It reads a whole line of text, spaces included, up to the new line. The new line is removed from the input but not stored.
cin >> stops at the first space, so it reads one word. getline() reads everything until the end of the line, so it can read a full name or sentence.
A earlier cin >> left a new line in the buffer. getline() reads that empty line first. Call cin.ignore(1000, '\n') before getline() to remove it.
std::getline(cin, str) fills a std::string that can grow. cin.getline(buf, n) fills a fixed char array and sets failbit if the line is too long.
Yes. Pass the character as the last argument, for example getline(in, s, ','). That is a handy way to split comma-separated text.

Next: good()

Check that no error or end-of-input flag is set.

good() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful