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