• c语言gets函数


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

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

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

    The line consists of all characters up to and including the first newline character (' '). gets then replaces the newline character with a null character ('') 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.

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

    [cpp] view plain copy
     
    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])!='';i++)                  //这里为什么不能使' '了???  
    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 . ",num);  
    19.     return 0;  
    20. }  


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

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

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

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


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

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

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

    [cpp] view plain copy
     
    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不等于空字符串,也不等于空字符串的地址。
    换成下面这条语句:

    [cpp] view plain copy
     
    1. while(strcmp(gets(buffer),"")!=0)  


    或者使用下面这段代码



    [cpp] view plain copy
     
    1.    char buffer[256];  
    2. gets(buffer);  
    3. while(strlen(buffer)!=0)  
    4. {  
    5.     puts(buffer);  
    6.     gets(buffer);  
    7. }  
    [cpp] view plain copy
     
      1. <pre></pre>  
      2. <pre></pre>  
      3. <pre></pre>  
      4. <pre></pre>  
      5. <pre></pre>  
      6.                       
  • 相关阅读:
    重写Override和重加载Overload
    Java 继承
    poj 3263 Tallest Cow
    矩阵求和
    全排列
    最大乘积
    三角形面积
    Sum
    搭积木
    调手表
  • 原文地址:https://www.cnblogs.com/dushikang/p/8328032.html
Copyright © 2020-2023  润新知