gets  fget的区别

   这两个函数都能从标准的输入流中读取信息,比如从键盘中输入信息,但是有些区别。

     使用gets当输入的字符多于预定个数时候,会造成溢出,程序报错。 

int main(int argc, const char * argv[]) {

    char search_for[];

    printf("Search for:");

    gets(search_for);

    printf("str is %s",search_for);

    return ;
}

    但是使用fgets能够解决这个问题,当超出时候,会自动截取。需要注意的地方是fgets只能获取n-1个字符,如下面的例子,字符数组的长度是10,但是能够接受的字符个数

只有9个,因为最后的一个字符结尾还需要一个结束符号'\0'.

    char search_for[];

    printf("Search for:");

    fgets(search_for,sizeof(search_for),stdin);

    printf("str is %s",search_for);

    return ;
}

    

gets--vs--fgets的更多相关文章

  1. php函数fgets读取文件

    如果一个文件比较大,可以考虑用fgets函数 下面是个例子: #文件作用:fgets读取文件 $start_time = microtime(true); $file_name = "a.t ...

  2. Linux C 字符串输入函数 gets()、fgets()、scanf() 详解

    一.gets() 函数详解 gets()函数用来从 标准输入设备(键盘)读取字符串直到 回车结束,但回车符('\n')不属于这个字符串. 调用格式为: gets(str); 其中str为字符串变量(字 ...

  3. c语言中gets ,getschar 和fgets 的用法及三者之间的差别,还有scanf

    ①gets [1]函数:gets(字符指针) [2]头文件:stdio.h(c中),c++不需包含此头文件 [3]原型:char*gets(char*buffer); [4]功能:从stdin流中读取 ...

  4. 函数fgets和fputs、fread和fwrite、fscanf和fprintf用法小结 (转)

    函数fgets和fputs.fread和fwrite.fscanf和fprintf用法小结 字符串读写函数fgets和fputs 一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符 ...

  5. 读取文件内容fopen,fgets,fclose

    <?php //首先采用“fopen”函数打开文件,得到返回值的就是资源类型.$file_handle = fopen("/data/webroot/resource/php/f.tx ...

  6. gets(),fgets()的作用机制探究

    gets(),fgets() scanf("%d",&a)若接受形如 2 这样的输入后,缓冲区内会留一个\n,此后若调用gets等函数时会读出这个换行出现错误,需注意 fg ...

  7. fgets函数

    打开文件 fopen("需要打开的路径") 然后使用fgets函数读取行 #include <stdio.h> #include <stdlib.h> #i ...

  8. hdu 1039 (string process, fgets, scanf, neat utilization of switch clause) 分类: hdoj 2015-06-16 22:15 38人阅读 评论(0) 收藏

    (string process, fgets, scanf, neat utilization of switch clause) simple problem, simple code. #incl ...

  9. hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏

    thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...

  10. fgets读取文件时的注意事项

    1 文本文件 a.txt 内容如下 2 c代码 FILE *fil; if (!(fil = fopen("/home/rudy/projects/paser/a.txt", &q ...

随机推荐

  1. Yar - Yet Another RPC framework for PHP

    Introduction Yar is a RPC framework which aims to provide a simple and easy way to do communication ...

  2. LINQ to Entities 不识别方法“System.String ToString()”,因此该方法无法转换为存储表达式。

    var data = DataSource.Skip(iDisplayStart).Take(iDisplayLength).Select(o => new { MatNR = o.MatNR, ...

  3. Android 字体颜色在一些机型上不适配(textcolor失效)

    最近在参加一个创业项目的开发,其中在适配Android4.4版本时候遇到一个问题,本身title是白色字体,并且标签栏里面的字是绿色的,但是到了4.4手机上就变成了黑色. 也就是说textcolor并 ...

  4. jquery实现表格行的动态增加和删除

    $("#Addmaterial").click(function () {//Addmaterial是增加按钮的ID $("#tab tr").attr(&qu ...

  5. 【Irrlicht鬼火引擎】 安装配置Irrlicht鬼火引擎

    一.下载引擎 官方网站:http://irrlicht.sourceforge.net/‎ 官方网站需要FQ才能进入,如果不想FQ,可以通过其他下载地址:CSDN下载:http://download. ...

  6. Scala中的If判断&While&For循环

    If 判断: object TestScalaIf { def main(args: Array[String]): Unit = { // val resutlt = judge1(-100) // ...

  7. 移动平台的meta标签-----神奇的功效(转)

    对于桌面平台web布局中大家对meta标签再熟悉不过了,它永远位于 head 元素内部,对做SEO的朋友一定对meta有种特殊的感情吧,今天我们就来说说移动平台的meta标签,在移动平台meta标签究 ...

  8. Java MongoDB Driver 3.x - Quick Start

    Maven Dependency: <dependency> <groupId>org.mongodb</groupId> <artifactId>mo ...

  9. Nginx - Additional Modules, Limits and Restrictions

    The following modules allow you to regulate access to the documents of your websites — require users ...

  10. MVC中提示错误:从客户端中检测到有潜在危险的 Request.Form 值的详细解决方法

    今天往MVC中加入了一个富文本编辑框,在提交信息的时候报了如下的错误:从客户端(Content="<EM ><STRONG ><U >这是测试这...&q ...