C + + string Getline () function Istream::get,getline function differs from string getline istream::read and Ostream::write __ functions

Source: Internet
Author: User

A useful string IO operation: getline. This function takes two parameters: an input stream object and a string object. The Getline function reads from the next line of the input stream and saves the read content to string, but does not include line breaks. Unlike the input operator, Getline does not ignore line breaks at the beginning of a row. As soon as Getline encounters a line break, even if it is the first character entered, Getline stops reading and returns. If the first character is a line feed, the string argument is set to an empty string.

The Getline function uses the IStream parameter as the return value, as well as the input operator as a judgment condition. For example:

int main ()
{
String line;
Read line at time until End-of-file
while (Getline (CIN, line))
cout << line << Endl;
return 0;
}

Because line does not contain a newline character, you need to add it yourself if you want to do it. As usual, use Endl to output a newline character to flush out the output buffer.

Note: The newline character will not be stored in a string object because the line break character is discarded when the Getline function returns.

Excerpt from "C + + Primer"

int get (); reads a character from the input stream and converts it back to int
istream& Istream::get (char& c); Reads a character from the input stream and saves it to C
istream& Istream::get (char* s, streamsize N);
Reads the n-1 character from the input stream or encounters the Terminator '/n ' or the end of the file, but ' n ' is not extracted from the input stream, the first character to be read the next time. At the end of S, add a '/0 '


istream& Istream::get (char* s, streamsize N, Char Delim);
Reads the n-1 character from the input stream or touches the Terminator Delim or file at the end, but Delim is not extracted from the input stream, the first character to be read the next time. Add a '/0 ' at the end of S

istream& Istream::getline (char* s, streamsize N);
istream& Istream::getline (char* s, streamsize N, Char Delim);
With istream& Istream::get (char* s, streamsize N); istream& Istream::get (char* s, streamsize N, Char Delim);
The difference is that Terminator will be extracted from the input stream, but will not be stored in S.

String Getline

istream& Getline (istream& is, string& str, char delim);
istream& Getline (istream& is, string& str);

Functions of the function and

istream& Istream::getline (char* s, streamsize N);
istream& Istream::getline (char* s, streamsize N, Char Delim);
The same

istream& Read (char* s, streamsize N);

Reads n characters from the input stream, or touches the file terminator but not the s last Plus '/0 ', so it's not a CString string

ostream& Write (char* s, streamsize N);

Writes n characters from the output stream, s does not need to be a CString string that is the '/0 ' end string

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.