c-为什么在gcc(linux)中考虑使用fgets(),使用它的替代方案是什么



可能重复:
为什么"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将缓冲区的大小作为其参数之一,如果正确使用,则不会出现此问题。

常见问题解答有一个包含更多详细信息的条目。

相关内容

最新更新