Using the C language gets () function error in Linux

Source: Internet
Author: User

Under Linux, use the

Gets (CMD)

Function error: warning:the ' gets ' function is dangerous and should isn't be used.

Workaround: Adopt

Fgets (Cmd,100,stdin);//100 is a size

Problem Solving!

Fgets reads characters from the stdin until the newline character or end of the file is read, but reads a maximum of size characters at a time. The read-out characters are stored in buffer cmd with a newline character. Returns a pointer to CMD.

Gets the line of information entered from the stdin into CMD, and then replaces the newline character with a string ending character, null. The user wants to ensure that the length of the buffer is greater than or equal to the maximum.

A detailed explanation of the gets:

char * gets (char * str);//Get string from stdin

Reads characters from stdin and stores them as a string into str until a newline character (' \ n ') or the End-of-file is R Eached.
The ending newline character (' \ n ') is not a included in the string.

A null character (' + ') is automatically appended after the last character copied to Str to signal the end of the C string .

Notice that gets does not behave exactly as Fgets does with stdin as Argument:first, the ending newline character are not Included with gets while with Fgets it is. And second, gets does not let you specify a limit in how many characters is to is read, so you must is careful with T He size of the array pointed by STR to avoid buffer overflows.

Description: The Red section explains well the "the ' function is dangerous and should not being used" for this warning reason.

Reference Link:http://www.cplusplus.com/reference/clibrary/cstdio/gets/

Using the C language gets () function error in Linux

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.