可能重复:
为什么"gets';功能是危险的?为什么不应该使用?
我建议用户使用fgets((输入一个字符串,该字符串将使用scanf((进行分析,以区分整数、浮点和字符。我想要一个可靠的程序,但我使用gcc:收到以下警告
在函数main':
: warning: the
gets中,函数是危险的,不应使用。
有人能告诉我为什么它很危险吗?除了它,什么是安全的替代品?如果有人能告诉我fgets((致命性的严重性,那将非常有帮助。
您可能有点困惑。简而言之:gets
是坏的,fgets
是好的。
man
页面解释了不应使用gets
的原因:
BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
fgets
将缓冲区的大小作为其参数之一,如果正确使用,则不会出现此问题。
常见问题解答有一个包含更多详细信息的条目。