C++ Gets() 函数在 Visual Studio 2015 社区中不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32283650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
gets() function is not available in Visual studio 2015 community
提问by maruf
I have faced a compiler error(c3861) in my newly installed Visual studio community 2015 IDE:
我在新安装的 Visual Studio 社区 2015 IDE 中遇到了编译器错误 (c3861):
I just want to use gets()function from stdio.h library, and i have included stdio.h
file in my program, but compiler show me a compiler error
like below:
我只想使用 stdio.h 库中的gets()函数,并且我已经stdio.h
在我的程序中包含了文件,但是编译器向我显示了一个编译器错误,如下所示:
error C3861: 'gets': identifier not found
What should i do to compile my program correctly withgets()
function.
我应该怎么做才能使用gets()
函数正确编译我的程序。
采纳答案by Basheer AL-MOMANI
if you are looking forward to learn
about
如果你正期待着learn
有关
buffer overflow vulnerability
缓冲区溢出漏洞
you simply can use it and anther unsafe functions by the fallowing steps
您只需通过以下步骤即可使用它和其他不安全的功能
- from the solution explorer right click on the project and choose properties
- navigate to Configuration Properties >> C/C++ >> Advanced
- change
Compile As
value toCompile as C Code (/TC)
- (optional) if you would like to disable the warning just put its warning number in
disable specific warning
- 在解决方案资源管理器中右键单击项目并选择属性
- 导航到配置属性 >> C/C++ >> 高级
- 将
Compile As
值更改为Compile as C Code (/TC)
- (可选)如果您想禁用警告,只需将其警告编号输入
disable specific warning
回答by Kurt Van den Branden
Since C11, gets
is replaced by gets_s
. The gets() function does not perform bounds checking, therefore this function is extremely vulnerable to buffer-overflows. The recommended replacements are gets_s()
or fgets()
由于 C11,gets
被替换为gets_s
。get() 函数不执行边界检查,因此该函数极易受到缓冲区溢出的影响。推荐的替代品是gets_s()
或fgets()
gets_s(buf);
fgets(buf, sizeof(buf), stdin);
回答by Bo Persson
The gets
function was considered too dangerous (because it can easilycause a buffer overflow), so it was removed from the latest revisions of both C and C++.
该gets
函数被认为太危险(因为它很容易导致缓冲区溢出),因此从 C 和 C++ 的最新版本中删除了它。
You are supposed to use fgets
instead. With that function you can limit input to the size of your buffer.
你应该fgets
改用。使用该功能,您可以将输入限制为缓冲区的大小。