getline
function <string>
istream& getline ( istream& is, string& str, char delim ); istream& getline ( istream& is, string& str );
Get line from stream
Extracts characters from
is and stores them into
str until a delimitation character is found.
从is中提取字符串并把它们存储到str中去,直到遇到定界符为止。
The delimiter character is delim for the first function version, and '/n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
The delimiter character is delim for the first function version, and '/n' (newline character) for the second. The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
在上面第一个函数原型中,定界符是delim,而在第二个函数原型中是‘/n'(换行符). 在这个操作的过程中,如果在is中遇到文件结束符或者别的错误情况发生,该函数也会停止。
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.
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.
如果定界符被发现,它就被提取或丢弃,也就是说,它如果没有被存储,在此之后它的下一个输入操作将会开始。
Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
注意它不像
istream::getline中的c-string版本,string版本用全局函数实现,而不是用stream类的成员函数。
Parameters 参数
-
is
istream object on which the extraction operation is performed.提取操作执行的istream对象
str
string object where the extracted content is stored.要存储内容的string对象
delim The delimiting character. The operation of extracting succesive characters is stopped when this character is read.
定界字符。当读到这个字符时,连续提取字符的操作将会被停止。
Return Value 返回值
The same as parameter is.Errors are signaled by modifying the internal state flags:
flag | error |
---|---|
eofbit | The end of the source of characters is reached during its operations. |
failbit | No characters were extracted because the end was prematurely found.Notice that some eofbit cases will also set failbit. |
badbit | An error other than the above happened. |
Additionaly, 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
1 2 3 4 5 6 7 8 9 10 11 | |
This example ilustrates how to get lines from the standard input stream ( cin ).
Basic template member declarations
( basic_istream<charT,traits> )1 2 3 4 5 6 7 8 9 | |
See also
operator>> | Extract string from istream (function) |
istream::getline | Get line from stream (public member function) |
getline
function
<string>
istream& getline ( istream& is, string& str, char delim ); istream& getline ( istream& is, string& str );
Get line from stream
Extracts characters from
is and stores them into
str until a delimitation character is found.
The delimiter character is delim for the first function version, and '/n' (newline character) for the second. 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.
Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
Errors are signaled by modifying the internal state flags:
Additionaly, 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.
This example ilustrates how to get lines from the standard input stream ( cin ).
The delimiter character is delim for the first function version, and '/n' (newline character) for the second. 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.
Notice that unlike the c-string versions of istream::getline, these string versions are implemented as global functions instead of members of the stream class.
Parameters
-
is
- istream object on which the extraction operation is performed. str
- string object where the extracted content is stored. delim
- The delimiting character. The operation of extracting succesive characters is stopped when this character is read.
Return Value
The same as parameter is.Errors are signaled by modifying the internal state flags:
flag | error |
---|---|
eofbit | The end of the source of characters is reached during its operations. |
failbit | No characters were extracted because the end was prematurely found.Notice that some eofbit cases will also set failbit. |
badbit | An error other than the above happened. |
Additionaly, 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
1 2 3 4 5 6 7 8 9 10 11 | |
This example ilustrates how to get lines from the standard input stream ( cin ).
Basic template member declarations
( basic_istream<charT,traits> )1 2 3 4 5 6 7 8 9 | |
See also
operator>> | Extract string from istream (function) |
istream::getline | Get line from stream (public member function) |