C++ std::getline() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::getline

Definition

In C++, std::getline() reads a full line from an input stream into a std::string.

By default it stops at '\\n' and discards that delimiter. Include <string> (and usually <iostream>).

This is a free function from <string> that works with std::string (and sometimes wstring). Member functions like find live on the string object with a dot; helpers like this are called as std::getline(...).

Syntax

Pass the stream and the string to fill.

C++
std::string line;
std::getline(std::cin, line);
std::getline(std::cin, line, ',');
CallWhat it does
std::getline(is, str)Read until newline into str.
std::getline(is, str, delim)Read until character delim.

There is also an istream member getline for raw char buffers. Prefer the free std::getline with std::string in modern C++.

Library declarations
C++
istream& getline(istream& is, string& str);
istream& getline(istream& is, string& str, char delim);

Parameters

ParameterDescription
isInput stream such as std::cin or an istringstream.
strDestination string (previous contents are replaced).
delimOptional stop character. Default is newline.

Return Value

Return ValueDescription
istream&The same stream. In a boolean context it is false on failure/EOF.

C++ Examples

Example 1 — Basic getline()

Read one line from an istringstream.

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

int main() {
    std::istringstream in("Hello world\\nNext");
    std::string line;
    std::getline(in, line);
    std::cout << line << '\\n';
    return 0;
}

How It Works

  1. The stream starts with a line of text.
  2. getline reads until newline, not stopping at spaces.
  3. Printed line includes the space.

Example 2 — getline With Comma

Split CSV-like text with a comma delimiter.

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

int main() {
    std::istringstream in("red,green,blue");
    std::string part;
    std::getline(in, part, ',');
    std::cout << part << '\\n';
    std::getline(in, part, ',');
    std::cout << part << '\\n';
    return 0;
}

How It Works

  1. First getline stops at the first comma.
  2. Second call continues and reads green.
  3. Delimiter commas are discarded.

Example 3 — Loop Until EOF

Read every line from a stream.

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

int main() {
    std::istringstream in("a\\nb\\nc\\n");
    std::string line;
    int n = 0;
    while (std::getline(in, line)) {
        ++n;
    }
    std::cout << n << '\\n';
    return 0;
}

How It Works

  1. The loop condition uses the stream returned by getline.
  2. Each successful read increments the counter.
  3. Three lines are counted.

Frequently Asked Questions

It reads characters from a stream into a std::string until a delimiter (default newline).
>> stops at whitespace, so it cannot read a full sentence with spaces.
In <string> for the std::string overloads.
It is extracted from the stream and discarded—not stored in the string.
A leftover newline remained in the buffer. Ignore it or use std::ws before getline.

Next: stod()

Keep going through the string functions. Convert a string to double.

stod() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

11 people found this page helpful