| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Global objects | ||||
| Member functions | ||||
(C++11) | ||||
| Formatted input | ||||
| Unformatted input | ||||
basic_istream::getline | ||||
| Positioning | ||||
| Miscellaneous | ||||
(C++11) | ||||
| Member classes | ||||
| Non-member functions | ||||
basic_istream& getline( char_type* s,std::streamsize count); | (1) | |
basic_istream& getline( char_type* s,std::streamsize count, char_type delim); | (2) | |
Extracts characters from stream until end of line or the specified delimiterdelim.
The first overload is equivalent togetline(s, count, widen('\n')).
Behaves asUnformattedInputFunction. After constructing and checking the sentry object, extracts characters from*this and stores them in successive locations of the array whose first element is pointed to bys, until any of the following occurs (tested in the order shown):
basic_istream::get()) and counted towardsgcount(), but is not stored.If the function extracts no characters, failbit is set in the local error state beforesetstate() is called.
In any case, ifcount>0, it then stores a null characterCharT() into the next successive location of the array and updatesgcount().
Contents |
Because condition #2 is tested before condition #3, the input line that exactly fits the buffer does not triggerfailbit.
Because the terminating character is counted as an extracted character, an empty input line does not triggerfailbit.
| s | - | pointer to the character string to store the characters to |
| count | - | size of character string pointed to bys |
| delim | - | delimiting character to stop the extraction at. It is extracted but not stored. |
*this
If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.
#include <array>#include <iostream>#include <sstream>#include <vector> int main(){std::istringstream input("abc|def|gh");std::vector<std::array<char,4>> v; // note: the following loop terminates when std::ios_base::operator bool()// on the stream returned from getline() returns falsefor(std::array<char,4> a; input.getline(&a[0],4,'|');) v.push_back(a); for(auto& a: v)std::cout<<&a[0]<<'\n';}
Output:
abcdefgh
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 531 | C++98 | std::getline could not handle thecase wherecount is non-positive | no character is extracted in this case |
| read data from an I/O stream into a string (function template)[edit] | |
| extracts formatted data (public member function)[edit] | |
| extracts characters (public member function)[edit] | |
| extracts blocks of characters (public member function)[edit] |