C++ Reference ยท Strings and I/O
C++ std::getline function
Reference for std::getline: read a whole line or any delimiter-separated field into a std::string, loop until EOF, handle blank lines, and the istream::getline variant.
std::getline (header <string>) reads characters from an input stream into a std::string until it reaches a delimiter, which is the newline by default. The delimiter is removed from the stream but is not stored in the string. Unlike >>, it keeps spaces, so it is the right tool for names, sentences and whole lines of a file.
You can supply a different delimiter, which turns getline into a splitter for comma-separated text: use it on a string stream containing the record. It works with any std::istream, including std::cin, file streams and string streams.
std::string line; while (std::getline(std::cin, line)) { // handle one line }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
std::istream& getline(std::istream& is, std::string& str); std::istream& getline(std::istream& is, std::string& str, char delim); // member functions of std::istream std::istream& getline(char* s, std::streamsize n); std::istream& getline(char* s, std::streamsize n, char delim);
| Name | Description |
|---|---|
is |
The stream to read from. |
str |
The string that receives the text. Its previous contents are replaced. |
delim |
The character that ends the read. Default '\n'. |
s, n |
For the member version: the buffer and its size. At most n - 1 characters are stored, followed by '\0'. |
Returns: the stream. It converts to false if nothing could be extracted (end of input before any character was read) or if an error occurred.
Errors / undefined behavior: the string version sets failbit when it extracts no characters, and eofbit when it hits the end of input. A final line without a newline is still read successfully; the next call fails. The member version sets failbit when the line is longer than n - 1 characters (the rest stays in the stream). An allocation failure or a stream with exceptions enabled can throw.
Usage notes
- Loop on the call.
while (std::getline(is, line))stops cleanly at the end of input; do not testeof()first. - Mixing with
>>. Aftercin >> xthe newline remains, so the nextgetlinereturns an empty string. Discard it withcin.ignore(...)orstd::ws. Seestd::cin. - Blank lines produce an empty string and succeed. Only "no characters at all" makes the call fail, so a delimiter immediately at the start still counts as an (empty) extraction.
- Windows line endings. In text mode a file's
\r\nbecomes\n, but data read in binary mode or from a network keeps the\rat the end of the string. Trim it when needed. - Trailing delimiter.
"a,b,"yields two fields, not three; an empty field in the middle ("a,,b") yields an empty string. - Prefer the string version over the
char*one: there is no size to guess and no truncation. - Limit. There is no maximum line length; a very long input line grows the string until memory runs out. Check
str.size()as you go for untrusted input, or use the member version with a bound. - C++ and C. The C equivalents are
fgets(keeps the newline) and POSIXgetline; the C++ function is not related to the POSIX one.
Related
std::cin,std::stringstream, Stream states,std::ifstream- Lessons: User input, File I/O