The Getline () function generates a string that contains a string of characters read from the input stream until the generation ends in the following cases.
1) to the end of the file,
2) encounter the delimiters of the function,
3) maximize input.
First, you must understand the purpose of designing the Getline function. In fact, it is very simple to read strings from the stream. There are also many reading methods, including the number of read characters based on the delimiter. From the name of this function, it intuitively means reading a row from the stream, but we should not be confused by this phenomenon.
C ++ has two Getline functions. One is defined in the string header file as a global function, and the function declaration is istream & Getline (istream & is, string & STR, char delim) and istream & Getline (istream & is, string & Str); the other is a member function of istream. The function declaration is istream & Getline (char * s, streamsize N) and istream & Getline (char * s, streamsize N, char delim ); note that the second Getline is to store the read string in the char array, but this parameter cannot be declared as the string type, because the C ++ compiler cannot execute this default conversion. Now let's look at the program: first look at the global function: the output is: From the function declaration, we can see that the differences between the two function declarations are mainly reflected in the number of parameters. If there are two parameters, the default qualifier is '\ n'. But if the qualifier is declared, is' \ n' still valid? I wrote the following program for testing: the output is: it can be seen that the linefeed is indeed invalid. Therefore, the Getline function has only one qualifier, Which is overwritten by each other.
Let's take a look at the Getline function of istream:
The output is: in fact, the Getline of istream is based on the Getline function of the global function, and an additional read termination condition is added, which is determined based on the number of read characters, actually, it reads n-1 characters, because the last place must be '\ 0. The two are basically the same elsewhere. The principle must be simple. Every time you call Getline, the object pointer keeps going down, which is equivalent to calling the get function and saving the read characters. When the number of qualifier or read characters meets the parameter requirements (or due to file reasons), the read is terminated. If the Delimiter is met, the character is extracted and discarded, that is, the file pointer is moved down, but the character is not saved, that is, after each Getline operation, the file pointer will stay behind the qualifier (in case of a qualifier ).