warning: the 'gets' function is dangerous and should not be used.的解决办法
来源:互联网 发布:树的遍历递归算法 编辑:程序博客网 时间:2024/06/05 05:55
在Linux下,使用
gets(cmd)
函数报错:warning: the 'gets' function is dangerous and should not beused.
问题出在程序中使用了 gets ,Linux 下gcc编译器不支持这个函数,解决办法是使用fgets
解决办法:采用
fgets(cmd,100,stdin);//100为size
问题解决!
fgets从stdin中读字符,直至读到换行符或文件结束,但一次最多读size个字符。读出的字符连同换行符存入缓冲区cmd中。返回指向cmd的指针。
gets把从stdin中输入的一行信息存入cmd中,然后将换行符置换成串结尾符NULL。用户要保证缓冲区的长度大于或等于最大的行长。
/* 代码实现 */#include <stdio.h>int main ( ) { char name[20]; printf("\n 输入任意字符 : "); fgets(name, 20, stdin);//stdin 意思是键盘输入 fputs(name, stdout); //stdout 输出 return 0;}
根据以上改动后,果然没有了warning,但是调试了n久的一个程序,确实怎么也没有正确结果,最后step跟踪,才发现了问题所在!那就是:
gets从终端读入是的字符串是用\0结束的,而fgets是以\n结束的(一般输入都用ENTER结束),然后strcmp两者的时候是不会相等的!
所以建议大家还是继续让它warning吧。。为了正确性!
这个问题主要应该是因为linux 和 windows的文件在换行符上编码不一样导致的,linux的换行是\0,windows的换行是\13\0,是两个字符。
但是的文件应该是在windows下编译的,所以导致会出现两字符不匹配。建议可以在linux终端下,利用dos2unix filename,来将换行符处理一下,应该就OK了
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- the `gets' function is dangerous and should not be used.
- warning: the `gets' function is dangerous and should not be used.
- warning: the `gets' function is dangerous and should not be used.
- “warning: the `gets' function is dangerous and should not be used.
- Linux下开发warning:"the `gets' function is dangerous and should not be used"
- warning: the `gets' function is dangerous and should not be used.
- warning: the `gets' function is dangerous and should not be used.
- warning: the `gets' function is dangerous and should not be used.
- warning: the ‘gets' function is dangerous and should not be used
- warning: the gets function is dangerous and should not be used.
- the update-modules command is deprecated and should not be used
- The function getUserId must be used with a prefix when a default namespace is not specified 解决办法
- 关于The function get must be used with a prefix when a default namespace is not specified的错误
- Why the "volatile" type class should not be used
- The function default must be used with a prefix when a default namespace is not specified
- Sencha Touch 2 toolbar 按钮模仿开始菜单
- OpenGL函数思考-glMatrixMode
- VC++WM_TIMER 定时器使用方法
- Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列教程
- spring学习----MethodInvokingFactoryBean(二)
- warning: the 'gets' function is dangerous and should not be used.的解决办法
- hdu 4382 模拟 矩阵连乘 高精度
- 单片机驱动DM9000网卡芯片(详细调试过程)
- myeclipse添加对hinernate4的hinernate.cfg.xml智能提示
- ora--12154 :TNS :could not resolve the connect identifier specified 错误处理
- 磁盘扇区
- 做好面试之c/c++系列---1
- Opcode features1.3
- 《Android学习指南》目录