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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:07:05  来源:igfitidea点击:

gets() function is not available in Visual studio 2015 community

c++visual-studio-2015

提问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.hfile 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 learnabout

如果你正期待着learn有关

buffer overflow vulnerability

缓冲区溢出漏洞

you simply can use it and anther unsafe functions by the fallowing steps

您只需通过以下步骤即可使用它和其他不安全的功能

  1. from the solution explorer right click on the project and choose properties
  2. navigate to Configuration Properties >> C/C++ >> Advanced
  3. change Compile Asvalue to Compile as C Code (/TC)
  4. (optional) if you would like to disable the warning just put its warning number in disable specific warning
  1. 在解决方案资源管理器中右键单击项目并选择属性
  2. 导航到配置属性 >> C/C++ >> 高级
  3. Compile As值更改为Compile as C Code (/TC)
  4. (可选)如果您想禁用警告,只需将其警告编号输入 disable specific warning

回答by Kurt Van den Branden

Since C11, getsis 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 getsfunction 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 fgetsinstead. With that function you can limit input to the size of your buffer.

你应该fgets改用。使用该功能,您可以将输入限制为缓冲区的大小。