(1) istream& getline (istream& is, string& str, char delim); istream& getline (istream&& is, string& str, char delim); (2) istream& getline (istream& is, string& str); istream& getline (istream&& is, string& str);
Get line from stream into string Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’, for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Note that any content in str before the call is replaced by the newly extracted sequence.
Each extracted character is appended to the string as if its member push_back was called.
Get/set content The first form (1) returns a string object with a copy of the current contents of the stream.
The second form (2) sets s as the contents of the stream, discarding any previous contents. The object preserves its open mode: if this includes ios_base::ate, the writing position is moved to the end of the new sequence.
Internally, the function calls the str member of its internal string buffer object.
Example
// stringstream::str #include <string> // std::string #include <iostream> // std::cout #include <sstream> // std::stringstream, std::stringbuf int main () { std::stringstream ss; ss.str ("Example string"); std::string s = ss.str(); std::cout << s << '\n'; return 0; }