禁用警告:通过头文件在 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屋!