C + + learning Getline () function reads a line of characters some input-related IStream class member functions

Source: Internet
Author: User

The purpose of the Getline function is to read a line of characters from the input stream, similar to the Get function with 3 parameters. That
Cin.getline (character array (or character pointer), number of characters N, terminating flag character)

[Example 13.7] reads a line of characters with the Getline function.

#include <iostream>using namespacestd;intMain () {Charch[ -]; cout<<"Enter a sentence:"<<Endl; CIN>>ch; cout<<"The string read with CIN is:"<<ch<<Endl; Cin.getline (CH, -,'/');//read a character or meet '/' Endcout<<"The second part is :"<<ch<<Endl; Cin.getline (CH, -);//read a character or meet '/n ' Endcout<<"The third part is :"<<ch<<Endl; return 0;}

The program runs as follows:
Enter a sentence:i like c++./i study c++./i am happy.
The string read with CIN is:i
The second part Is:like C + +.
The third part IS:I study c++./i am H

Please carefully analyze the results of the operation. Use "cin>>" to extract data from the input stream and terminate with a space. Therefore, only one character ' I ' is read, stored in the character array element ch[0], and then stored in ch[1]. So with "cout<<ch" output, only one character ' I ' is output. Then use Cin.getline (CH, 20, '/') to read 19 characters from the input stream (or the end of the event). Note: The data is not read at the beginning of the input stream. There is a character pointer in the input stream that points to the character that is currently being accessed. At the beginning, the pointer points to the first character, after reading the first character ' I ', the pointer moves to the next character (the space after the ' I '), so the Getline function reads from the space, encounters the stop, and puts the string "like C + +." 10 array elements stored in ch[0], and then output these 10 characters with "Cout<<ch". Note: Stop reading when the terminating flag character "/" is not placed in the array. Then use Cin.getline (CH, 20) to read 19 characters (or the end of '/n '), because not specified with '/' as the end flag, so the 2nd '/' is read as a general character, read into 19 characters, and finally output the 19 characters.

There are a few explanations and ask the reader to think:
1) If a 2nd Cin.getline function is also written in Cin. Getline (CH, 20, '/'), what will the output be? The output of the last line at this point is:
The third part IS:I study C + +.

2) If you read the data from the input stream with Cin.getline (CH, 20, '/') and enter the return key ("\ n"), do you want to end the read? The conclusion is that at this time "\ n" is not the end sign "\ n" is read as a character.

3) When using the Getline function to read a character from the input stream, the end of the terminating glyph is encountered, and the pointer moves to the terminating flag character, the next Getline function starts reading from the next character of the terminating flag, as shown in the result of the program's operation. If the Cin.get function reads a character from the input stream, it stops reading when the terminating glyph is encountered, the pointer does not move backwards, and remains in the original position. The next read will still start with the terminating flag character. This is where the Getline function differs from the GET function. If the two Cin.line function calls in the example 13.7 program are changed to the following function call:
Cin.getline (CH, 20, '/');
The result of the operation is:
Enter a sentence:i like c++./i study c++./i am happy.
The string read with CIN is:i
The second part Is:like C + +.
The third part is: (no valid characters are read from the stream)

A 2nd cin. Getline (CH, 20, '/') reads the character from the current position of the pointer, and the 1th character encountered is the end of the terminating flag character read-only and the "\" is stored in the ch[0], so there is no character output with "cout<<ch" output.

Therefore, use the GET function to pay special attention to, if necessary, other methods to skip the terminating flag character (such as the Ignore function described later, see: Some of the input-related IStream class member functions), but generally still with the Getline function more convenient.

4) Compare the differences between reading data with "cin<<" and using the member function Cin.getline (). Use "cin<<" to read the data with white space characters (including space, TAB key, return) as the terminating flag, while reading the data with Cin.getline () read a series of characters consecutively, can include spaces. With "Cin <<" You can read all types of data in C + + standard type (if overloaded, you can also use to enter data for custom types), and Cin.getline () is used only for inputting character data.

Some IStream class member functions related to the input

In addition to the member functions described earlier for reading data, the IStream class has other member functions that are useful when entering data.

EOF function

EOF is an abbreviation for end of file, which means "end of files". Reads data from the input stream, if it reaches the end of the file (in case of a file terminator), the EOF function value is not 0 value (true), otherwise 0 (false).

[Example 13.8] reads one line of characters, and outputs non-whitespace characters.

#include <iostream>usingnamespace  std; int Main () {   char  C;     while (!cin.eof ())  // EOF () is false to indicate that no file Terminator   was encountered if (C=cin. Get ())! =")  // Check if the character being read in is a space character       cout.put (c   ); return 0 ;}
Peek function

Peek is the meaning of "observation", the function of peek is to observe the next character. Its invocation form is:
C=cin.peek ();
The return value of the function is the current character pointed to by the pointer, but it is only observed, and the pointer remains at the current position and does not move back. If the character you want to access is a file terminator, the function value is EOF (-1).

putback function

Its invocation form is
Cin.putback (CH);
The function is to return the character Ch read from the input stream preceded by a GET or getline function to the input stream, which is inserted into the current pointer position for later reading.

[Example 13.9] The use of the Peek function and the putback function.

#include <iostream>using namespacestd;intMain () {Charc[ -]; intch; cout<<"Please enter a sentence:"<<Endl; Cin.getline (c, the,'/'); cout<<"The first part is :"<<c<<Endl; CH=cin.peek ();//View current charactercout<<"The next character (ASCII code) is:"<<ch<<Endl; Cin.putback (c[0]);//insert ' I ' to the point where the pointer is pointingCin.getline (c, the,'/'); cout<<"The second part is :"<<c<<Endl; return 0;}

The operating conditions are as follows:
Please enter a sentence:
I am a boy./am a student./
The first part is:i is a boy.
The next character (ASCII code) is:32 (the next character is a space)
The second part is:i am a student

Ignore function

Its invocation form is
Cin.ignore (n, terminating character)
The function is to skip n characters in the input stream, or to end prematurely when a specified terminating character is encountered (a number of characters, including the terminating character, are skipped at this time). Such as
Ighore (5, ' a ')//skip the input stream characters, after ' A ' no longer jump
You can also take no parameters or only one parameter. Such as
Ignore ()//n default value, terminating word defaults think EOF
Equivalent
Ignore (1, EOF)

[Example 13.10] skips the characters in the input stream with the Ignore function. First look at the case without the Ignore function:

#include <iostream>using namespacestd;intMain () {Charch[ -]; Cin.Get(CH, -,'/'); cout<<"The first part is :"<<ch<<Endl; Cin.Get(CH, -,'/'); cout<<"The second part is :"<<ch<<Endl; return 0;}

The results of the operation are as follows:
I like c++./i study c++./i am happy.
The first part is:i like C + +.
The second part is: (character array ch does not read valid characters from the input stream)

If you want the second cin.get function to read "I study C + +.", you should try to skip the first '/' in the input stream and use the Ignore function to do this and change the program to:

#include <iostream>using namespacestd;intMain () {Charch[ -]; Cin.Get(CH, -,'/'); cout<<"The first part is :"<<ch<<Endl; Cin.ignore ();//skipping one character in the input streamCin.Get(CH, -,'/'); cout<<"The second part is :"<<ch<<Endl; return 0;}

The results of the operation are as follows:
I like c++./i study c++./i am happy.
The first part is:i like C + +.
The second part IS:I study C + +.

The various member functions described above can be invoked not only with the cin stream object, but also with other stream objects of the IStream class.

C + + learning Getline () function reads a line of characters some input-related IStream class member functions

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.