Home C++ Strings Functions getline() C++ std::getline() Function 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.
std::string line;
std::getline(std::cin, line);
std::getline(std::cin, line, ','); Call What 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 istream& getline(istream& is, string& str);
istream& getline(istream& is, string& str, char delim); Parameters Parameter Description isInput stream such as std::cin or an istringstream. strDestination string (previous contents are replaced). delimOptional stop character. Default is newline.
Return Value Return Value Description istream&The same stream. In a boolean context it is false on failure/EOF.
C++ Examples 1. From stringstream 2. Custom delimiter 3. Read all lines Example 1 — Basic getline() Read one line from an istringstream.
#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 The stream starts with a line of text. getline reads until newline, not stopping at spaces. Printed line includes the space. Example 2 — getline With Comma Split CSV-like text with a comma delimiter.
#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 First getline stops at the first comma. Second call continues and reads green. Delimiter commas are discarded. Example 3 — Loop Until EOF Read every line from a stream.
#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 The loop condition uses the stream returned by getline. Each successful read increments the counter. Three lines are counted. Frequently Asked Questions What does std::getline do? It reads characters from a stream into a std::string until a delimiter (default newline).
Why not use cin >> ? >> stops at whitespace, so it cannot read a full sentence with spaces.
Where is getline declared? In <string> for the std::string overloads.
What happens to the delimiter? It is extracted from the stream and discarded—not stored in the string.
Why did getline read an empty line after >> ? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion