函数gets的原型为:char*gets(char*buffer);

在 stdio.h中定义,如果要程序中用到此函数需包含#include<stdio.h>

gets()函数用来从标准输入设备(键盘)读取字符串直至接受到换行符或EOF时停止结束,并将读取的结果存放在buffer指针所指向的字符数组中,但换行符会被丢弃,然后在末尾添加'\0'字符。

The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line. In contrast, the fgets function retains the newline character. _getws is a wide-character version of gets; its argument and return value are wide-character strings.

-----from msdn

调用格式为:gets(s);   其中s为字符串变量(字符串数组名或字符串指针)。

返回值:读入成功,返回与参数buffer相同的指针;读入过程中遇到EOF(End-of-File)或发生错误,返回NULL指针。所以在遇到返回值为NULL的情况,要用ferrorfeof函数检查是发生错误还是遇到EOF。

gets(s)函数与 scanf("%s",s) 相似,但不完全相同,使用scanf("%s",s) 函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。

注意:本函数可以无限读取,不会判断上限,所以程序员应该确保buffer的空间足够大,以便在执行读操作时不发生溢出。为了避免这种情况,我们可以用fgets()来替换gets()。

宽字符版本,当使用unicode宽字符文本时,使用这个函数 _getws();

【百科查阅】

注意编译的时候这段警告信息:
warning C4996: 'gets': This function or variable may be unsafe. Consider using gets_s instead. To disable deprecation,use _CRT_SECURE_NO_WARNINGS. See online help for details.

贴子中的一段代码,也粘到这里看下了

  1. #include <stdio.h>
  2. int main ()
  3. {
  4. char string[100];
  5. int i,num=0,word=0;
  6. char c;
  7. gets(string);
  8. for (i=0;(c=string[i])!='\0';i++)                  //这里为什么不能使'\n'了???
  9. {
  10. if (c==' ')
  11. word=0;
  12. else if(word==0)
  13. {
  14. word=1;
  15. num++;
  16. }
  17. }
  18. printf ("there are %d words in this line .\n",num);
  19. return 0;
  20. }

上述理论中msdn中的一段正好说明了这个知识点:

The line consists of all characters up to and including the first newline character ('\n'). gets then replaces the newline character with a null character ('\0') before returning the line.

--------------------------------------------------------------

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char buffer[21];  //20 chars + '\0'
  5. gets(buffer);
  6. //danger :there is no way to limit the number of characters
  7. printf("You input was %s\n",buffer);
  8. system("pause");
  9. return 0;
  10. }

这段代码中无法保证缓冲区是否足够大会引起溢出而出错。

----------------------------------------------------------

下面这段测试代码当输入空串按回车()不能正常的结束循环。

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4. char buffer[256];
  5. while(gets(buffer)!=NULL)
  6. {
  7. puts(buffer);
  8. }
  9. return 0;
  10. }

原因在于while(gets(buffer)!=NULL),要使结束循环,需要gets调用失败。
gets返回值是一个指针,也就是输入字符串的地址,而NULL不等于空字符串,也不等于空字符串的地址。
换成下面这条语句:

  1. while(strcmp(gets(buffer),"")!=0)

或者使用下面这段代码

  1. char buffer[256];
  2. gets(buffer);
  3. while(strlen(buffer)!=0)
  4. {
  5. puts(buffer);
  6. gets(buffer);
  7. }
    1. <pre></pre>
    2. <pre></pre>
    3. <pre></pre>
    4. <pre></pre>
    5. <pre></pre>

最新文章

  1. Axure 自适应视图
  2. jQuery+CSS3实现404背景动画特效
  3. Android应用反破解的思路
  4. shell脚本批量处理字符串
  5. stopImmediatePropagation的应用
  6. DB2数据库管理最佳实践(1)
  7. 生成guid
  8. 使用grep查找文件中指定字符出现的次数
  9. UVA 12898 And Or 数学暴力
  10. c++ uuid生成法则
  11. eclipse引用头文件报错问题-解决方法
  12. Sublime Text 中使用Git插件连接GitHub
  13. ANDROID_MARS学习笔记_S01原始版_009_下载文件
  14. poj 3735 大数量反复操作问题(矩阵高速幂)
  15. 用C#实现网络爬虫(二)
  16. lua 怎样输出树状结构的table?
  17. maven 聚合工程的创建和打包
  18. Java异常体系简析
  19. Dynamics CRM2016 Web API之删除
  20. Mongodb注入

热门文章

  1. js中map()、some()、every()、filter()的区别
  2. android开发之java代码中如何获取到当前时间。详情代码带注释。
  3. UGUI核心元素、基本控件、复合控件和高级控件
  4. Python之根据四个坐标确定其位于左上下右上下
  5. Activiti7 zip部署,查询及其删除
  6. H5选择器
  7. URL 传参转义 (特殊符号转义)
  8. Jenkins下Vue自动部署(二)
  9. Java并发编程之闭锁与栅栏
  10. oracle之二表的几种类型