在Linux下,使用

gets(cmd)

函数报错:warning: the 'gets' function is dangerous and should not be used.

解决办法:采用

fgets(cmd,100,stdin);//100为size

问题解决!

fgets从stdin中读字符,直至读到换行符或文件结束,但一次最多读size个字符。读出的字符连同换行符存入缓冲区cmd中。返回指向cmd的指针。

gets把从stdin中输入的一行信息存入cmd中,然后将换行符置换成串结尾符NULL。用户要保证缓冲区的长度大于或等于最大的行长。

gets的详细解释:

char * gets ( char * str );//Get string from stdin

Reads characters from stdin and stores them as a string into str  until a newline character ('\n') or the End-of-File is reached.
The ending newline character ('\n') is not included in the string.

A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.

Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.

说明:红色部分很好地解释了“the 'gets' function is dangerous and should not be used”这个warning的原因。

参考链接:http://www.cplusplus.com/reference/clibrary/cstdio/gets/

Linux 下使用C语言 gets()函数报错的更多相关文章

  1. Linux下使用JDK11部署Nacos启动报错:Could not find or load main class

    Linux下使用JDK11部署Nacos 错误日志 /nacos/jdk-11.0.12/bin/java -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize ...

  2. Linux下Python3.5使用pyqt5.11报错 ImportError: /usr/local/lib/python3.5/dist-packages/PyQt5/QtCore.so: undefined symbol: PySlice_AdjustIndices 解决方法

    我用的Linux自带的是Python3.5版本,运行pip3 install PyQt5, 下载的是PyQt5.11,运行PyQt5程序会报错: ImportError: /usr/local/lib ...

  3. linux下安装apache最常见的报错解决

    报错如下: Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, ...

  4. VS中C语言scanf函数报错

    在VS中创建C项目使用scanf方法时会如下报错 解决方案 方法1:文件中设置 在第一行设置代码    #define _CRT_SECURE_NO_WARNINGS 或者添加警告忽略     #pr ...

  5. 在linux下运行apt-get update 时,报错/var/lib/apt/lists/lock

    在运行apt-get update 时,报下面的错误: E: 无法获得锁 /var/lib/apt/lists/lock - open (11: Resource temporarily unavai ...

  6. Linux下rm -rf删除文件夹报错_ Device or resource busy

    1.错误信息 rm: cannot remove `speechd-centos_6.2-prtl-pred-mf34/modules/t2p/py/third/g2p/.nfs00000000039 ...

  7. 1、linux下mysql5.5.20安装过程报错汇总

    1.Access denied for user 'root'@'localhost' (using password: YES) 这个提示是因为root帐户默认不开放远程访问权限,所以需要修改一下相 ...

  8. Linux下Java运行.class文件,报错找不到或无法加载主类

    classpath配置的错误,所以找不到.class文件. 原先的etc/profile中的classpath配置 export CLASSPATH=$JAVA_HOME/lib/tools.jar ...

  9. windows下的c语言和linux 下的c语言以及C标准库和系统API

    1.引出我们的问题? 标准c库都是一样的!大家想必都在windows下做过文件编程,在linux下也是一样的函数名,参数都一样.当时就有了疑问,因为我们非常清楚 其本质是不可能一样的,源于这是俩个操作 ...

随机推荐

  1. 2016.6.20 eclipse安装完毕后打开失败,显示a JDK or a JRE must be avaliable in order to run Eclispe

    下载完成后,点击eclipse.exe,跳出如下错误. 按照百度的方法,修改了一下eclispe的配置文件,不仅没解决问题,结果跳出另一个新的错误: Failed to load the JNI sh ...

  2. vuex 深入理解

    参考自:https://mp.weixin.qq.com/s?src=11&timestamp=1528275978&ver=922&signature=ZeHPZ2ZrLir ...

  3. 身份证号码正则匹配-javascript

    function a(a, b) { return a.test(b) } function b(a) { return a = jQuery.trim(a), 0 == a.length } fun ...

  4. pom.xml基础配置

    pom.xml基础配置: maven中,最让我迷惑的还是那一堆配置! 就拿这个属性配置来说: 我需要让整个项目统一字符集编码,就需要设定 <project.build.sourceEncodin ...

  5. iOS 振动反馈

    代码地址如下:http://www.demodashi.com/demo/12461.html 1. 常用场景 继 iPhone7/7P 实体 home 键出现后,home 键再也无法通过真实的物理按 ...

  6. Linux系统下授权MySQL账户访问指定数据库和数据库操作

    Linux系统下授权MySQL账户访问指定数据库 需求: 1.在MySQL中创建数据库mydata 2.新建MySQL账户admin密码123456 3.赋予账户admin对数据库mydata具有完全 ...

  7. php的特殊功能-----不是和其他语言比较

    1.header(); 他不只是重定向,和更改字符集 而是发送表头,如 header('HTTP/1.1 404 Not Found   gfdgd'); 可以发送信息给浏览器,让浏览器显示404错误 ...

  8. Redis闲谈(1):构建知识图谱

    场景:Redis面试 (图片来源于网络) 面试官: 我看到你的简历上说你熟练使用Redis,那么你讲一下Redis是干嘛用的? 小明: (心中窃喜,Redis不就是缓存吗?)Redis主要用作缓存,通 ...

  9. ReentrentLock重入锁

    ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...

  10. The Google File System论文拜读

    The Google File System Sanjay Ghemawat, Howard Gobioff, and Shun-Tak Leung Google∗ 摘要 我们设计并实现了谷歌文件系统 ...