istream::getline

public member function
istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

Get line from stream

Extracts characters from the input sequence and stores them as a c-string into the array beginning at s.

Characters are extracted until either (n - 1) characters have been extracted or the delimiting character is found (which is delim if this parameter is specified, or '\n' otherwise). The extraction also stops if the end of file is reached in the input sequence or if an 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. If you don't want this character to be extracted, you can use member get instead.

The ending null character that signals the end of a c-string is automatically appended to s after the data extracted.

The number of characters read by this function can be obtained by calling to the member function gcount.

A global function with the same name exists in header <string>. This global function provides a similar behavior, but with standard C++ string objects instead of c-strings: see getline (string).

Parameters

s
A pointer to an array of characters where the string is stored as a c-string.
n
Maximum number of characters to store (including the terminating null character).
This is an integer value of type streamsize.
If the function stops reading because this size is reached, the failbit internal flag is set.
delim
The delimiting character. The operation of extracting succesive characters is stopped when this character is read. This parameter is optional, if not specified the function considers '\n' (a newline character) to be the delimiting character.

Return Value

The function returns *this.

Errors are signaled by modifying the internal state flags:

flagerror
eofbit The end of the source of characters is reached during its operations.
failbit No characters were extracted because the end was prematurely found.
This is also set if the function stops extracting because n-1 characters were extracted (n including the terminating null-character).
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 member function ios::exceptions, an exception of type ios_base::failure is thrown.

Example

// istream getline
#include <iostream>
using namespace std;

int main () {
char name[256], title[256];

cout << "Enter your name: ";
cin.getline (name,256);

cout << "Enter your favourite movie: ";
cin.getline (title,256);

cout << name << "'s favourite movie is " << title;

return 0;
}

This example ilustrates how to get lines from the standard input stream ( cin ).

Basic template member declarations

( basic_istream<charT,traits> )

typedef charT char_type;
basic_istream& getline (char_type* s, streamsize n );
basic_istream& getline (char_type* s, streamsize n, char_type delim );

See also

istream::get Get unformatted data from stream (public member function)
istream::ignore Extract and discard characters (public member functions)
istream::gcount Get number of characters extracted by last unformatted input operation (public member function)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     

getline为全局方法

getline function

istream& getline ( istream& is, string& str, char delim );
istream& getline ( istream& is, string& str );
<string>

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.

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:

flagerror
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 // getline with strings
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main () {
 7   string str;
 8   cout << "Please enter full name: ";
 9   getline (cin,str);
10   cout << "Thank you, " << str << ".\n";
11 }

This example ilustrates how to get lines from the standard input stream ( cin ).

Basic template member declarations

( basic_istream<charT,traits> )

template<class charT, class traits, class Allocator>
basic_istream<charT,traits>&
getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Allocator>& str,
charT delim );
template<class charT, class traits, class Allocator>
basic_istream<charT,traits>&
getline (basic_istream<charT,traits>& is,
basic_string<charT,traits,Allocator>& str );

See also

operator>> Extract string from istream (function)
istream::getline Get line from stream (public member function)

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

Posted on 2009-06-16 19:18 Liu 阅读(2558) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理