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

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 | Call | What 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 |