禁用警告:通过头文件,“获取"功能在GCC中是危险的吗? [英] Disable warning: the `gets' function is dangerous in GCC through header files?
问题描述
我在C代码中使用函数 gets()
.我的代码运行正常,但收到警告消息
I am using the function gets()
in my C code.
My code is working fine but I am getting a warning message
(.text+0xe6): warning: the `gets' function is dangerous and should not be used.
我希望此警告消息不弹出.有什么办法吗?
I want this warning message not to pop up. Is there any way?
我想知道是否可能通过创建用于禁用某些警告的头文件来实现这种可能性.还是在编译过程中有任何选项可以满足我的目的?还是可能有一种使用 gets()
的特殊方式来使此警告不弹出?
I am wondering that there might be such possibilities by creating a header file for disabling some warnings. Or is there any option during compiling that can serve my purpose? Or may be there is a particular way of using gets()
for this warning not to pop up?
推荐答案
显而易见的答案是从编译器试图告诉您的内容中学习-绝对不要使用gets(),因为它是完全不安全的.改用 fgets(),这样可以防止缓冲区溢出.>
The obvious answer is to learn from what the compiler is trying to tell you - you should never, ever, use gets(), as it is totally unsafe. Use fgets() instead, which allows you to prevent possible buffer overruns.
#define BUFFER_SIZE 100
char buff[BUFFER_SIZE];
gets( buff); // unsafe!
fgets( buff, sizeof(buff), stdin ); // safe
这篇关于禁用警告:通过头文件,“获取"功能在GCC中是危险的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!