(1) | istream& getline (istream& is, string& str, char delim); |
---|---|
(2) | istream& getline (istream& is, string& str); |
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).
从is对象中提取字符并且把他们保存到str的字符串中,直到遇到定界符delim(为自己定义)(或者'\n')。
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.
注意,str中的字符串会被新输入的序列所代替。
Each extracted character is appended to the string as if its member push_back was called.
如果成员函数push_back被调用了,所有字符会被追加到字符串后面
Parameters
-
is
- istream object from which characters are extracted. str
-
string object where the extracted line is stored.
The contents in the string before the call (if any) are discarded and replaced by the extracted line.
-
之前存放的字符串会被释放并且会被后面的新的一行输入所取代。
Return Value
The same as parameter is.A call to this function may set any of the internal state flags of is if:
flag | error |
---|---|
eofbit | The end of the source of characters is reached during its operations. |
failbit | The input obtained could not be interpreted as a valid textual representation of an object of this type. In this case, distr preserves the parameters and internal data it had before the call. Notice that some eofbit cases will also set failbit. |
badbit | An error other than the above happened. |
Additionally, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
| |