First, let's look at the function signatures.
istream& getline( istream& is, string &str );
istream& getline( istream& is, string &str, char delim );
istream& istream::getline( char *s, streamsize n );
istream& istream::getline( char *s, streamsize n, char delim );
For both functions, the delimiter character is delim and '\n' (newline character) is the default value. The extraction also stops if the end of file is reached or if some other error occurs during the input operation. For the function istream::getline, at most \(n-1\) characters can be read and the ending null character will be automatically appended to the C-style string after data extraction.
For both functions, 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.
Differences:
- getline is a global function included in the header <string>, while istream::getline is a member function of class istream.
- getline reads characters into a C++ string type string, while istream::getline reads characters into a C-style string.
References:
C++ Reference: getline
C++ Reference: istream::getline
No comments:
Post a Comment