Differences between istream get and Getline

Source: Internet
Author: User

References:
Http://blog.163.com/huang_zhong_yuan/blog/static/1749752832010102223333176/

The get function can obtain a single character, a specified length string, and so on each time.
The Getline function obtains a row each time, or extracts strings separated by specified delimiters.

Get function prototype:

int get();istream& get ( char& c );istream& get ( char* s, streamsize n );istream& get ( char* s, streamsize n, char delim );istream& get ( streambuf& sb);istream& get ( streambuf& sb, char delim );

Http://www.cplusplus.com/reference/iostream/istream/get/

Getline function prototype:

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

Http://www.cplusplus.com/reference/iostream/istream/getline/

We can see that both functions can extract strings Based on the specified delimiter.

Differences:
When the get function encounters a delimiter, it stops obtaining the delimiter and leaves it in the input stream. When the Getline function encounters a delimiter, it stops obtaining the delimiter, but extracts it from the input stream.
Therefore, when using the get function, you need to manually skip the delimiter, while Getline does not.

Sample Code:

# Include <iostream> # include <string> # include <sstream> int main () {int length; STD: String ST = "enter, the, name, of,, existing, text, file: "; STD: istringstream (ST); int I = 0; char array [20] = {0}; while (stream. get (array, 20, ',') {// get the current position length = stream. tellg (); STD: cout <array <STD: Endl; // skip the comma (,) stream. seekg (Length + 1, STD: IOS: Beg);} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.