Fgets () and gets ()

Source: Internet
Author: User
First look at the function prototype: char* fgets (char *s, int n, FILE *stream); Char* gets (char *s); The fgets function is generally used to read the n-1 characters in the file into S, in fact, this function also has a good use is to read the string from the standard input stream, and do not worry about the number of characters entered beyond the size of the character array caused the overflow problem. How to do it. as follows: Char str[10]; Fgets (str, siezof (str), stdin); If you use the gets function, there is no guarantee that the above problems will not occur (the number of characters entered exceeds the size of the character array resulting in overflow) do not believe you can run the following code: Char str[10]; Gets (str); Of course you have to enter more than 10 characters to see the problem here, the benefits of fgets () no longer need to elaborate. It is noteworthy to note that fgets () reads only n-1 characters. Therefore, fgets () reads the line break, the end of the file, or reads the n-1 character and returns

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.