问题描述
通过
最近C标准的修订(2011年)明确了
从规范中删除此功能。
该功能是在C ++ pcated德$ P $(截至2011标准,如下
C99 + TC3)。
The function is deprecated in C++ (as of 2011 standard, which follows C99+TC3).
我只是想知道什么是替代获得()
在C11标准?
I just wanted to know what is the alternative to gets()
in C11 standard?
推荐答案
在C11 获得
已被 gets_s 具有以下声明:
In C11
gets
has been substituted by gets_s
that has the following declaration:
char *gets_s(char *str, rsize_t n);
此功能将最多读
N-1
从标准输入
字符到 * STR
。这是为了避免固有的获得
的缓冲区溢出漏洞。功能与fgets
也是一种选择。从:
This function will read at most
n-1
chars from stdin
into *str
. This is to avoid the buffer overflow vulnerability inherent to gets
. The function fgets
is also an option. From http://en.cppreference.com/w/c/io/gets:
在获得()函数不执行边界检查,因此该功能非常容易受到缓冲区溢出攻击。它不能安全使用(除非程序制约有什么可以读取标准出现的环境中运行)。出于这个原因,该功能已经在C11标准在第三更正C99标准pcated和完全删除德$ P $。 与fgets()和 gets_s()是推荐替代。
切勿使用获得()。
由于
gets_s
中的扩展,标准的定义,只是可选的实施,你应该使用编写的程序与fgets
来代替。如果你使用与fgets
在标准输入
你的计划也将编制在早期版本的C.但是要记住的差异行为:当 gets_s
已经阅读 N-1
字符它保持阅读,直到一个新行或端值的文件达到,丢弃的投入。所以,用 gets_s
你总是在阅读一整行,哪怕只是它的一部分可以在输入缓冲区中返回。
Given that
gets_s
is defined in an extension to the standard, only optionally implemented, you should probably write your programs using fgets
instead. If you use fgets
on stdin
your program will also compile in earlier versions of C. But keep in mind the difference in the behavior: when gets_s
has read n-1
characters it keeps reading until a new line or end-of-file is reached, discarding the input. So, with gets_s
you are always reading an entire line, even if only a part of it can be returned in the input buffer.
这篇关于什么是得到()相当于C11?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!