Row reads in c/A + +

Source: Internet
Author: User

In C language has been very easy to confuse, get and fgets the difference:

char * fgets (char * str, int num, FILE * stream);
Reads characters from stream and stores them as a C string to str until (num-1) characters ha ve been read or either a newline or the end-of-file is reached, whichever happens first.

A newline character makes fgets stop reading, but it's considered a valid character by the function and included The string copied to str.

A terminating null character is automatically appended after the characters copied to str.

Notice that fgets was quite different from Gets:not only fgets accepts a stream argument, but Also allows to specify the maximum a size of str and includes in the string any ending newline character.

char * gets (char * str);
Get string from stdin
Reads characters from the standard input (stdin) and stores them as a C string to str until a newline Character or theEnd-of-file is reached.

The newline character, if found, is isn't copied into str.

A terminating null character is automatically appended after the characters copied to str.

Notice that 's quite different from Fgets:not only gets uses stdin as source, but it does not incl Ude the ending newline character in the resulting string and does don't allow to specify a maximum size for str (WH Ich can leads to buffer overflows).

In short, fgets is able to specify the maximum number of input characters (num-1), and they all terminate after encountering a newline character, Fgets also reads the newline character into the string, and gets a simple drop-line character.

There are four functions in C + + that correspond to CIN:

IStream & Get (char *, int, char);
IStream & Get (char *, int);
IStream & Getline (char *, int, char);
IStream & Getline (char *, int);

Here, both get and getline do not read the newline character, but get will leave the newline character (or the other delimiter specified) in the stream, and Getline will read the newline character from the stream and discard it.



Row reads in c/A + +

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.