zoukankan      html  css  js  c++  java
  • 【C语言天天练(十五)】字符串输入函数fgets、gets和scanf

    引言:假设想把一个字符串读到程序中。必须首先预留存储字符串的空间。然后使用输入函数来获取这个字符串。


    读取字符串输入的第一件事是建立一个空间以存放读入的字符串。

    char *name;

    scanf("%s", name);

    这段代码尽管可能通过编译,但由于name能够指向不论什么地方,所以它的输入值可能覆盖曾经name所指位置的值。

    解决的方法是声明一个固定大小的字符数组,或者使用C库里的分配存储空间的函数


    1、gets函数从系统标准输入获得一个字符串。读取字符串直到遇到一个换行符( )。它读取换行符之前的全部字符。在这些字符后加入一个空字符(),然后把这个字符串交给调用它的程序。它把读取的换行符直接丢弃。而不是把它放入字符串中,这与以下讨论的fgets函数不同,以下再给出样例证明这一点。

    #include <stdio.h>

    char *gets(char *s);

    演示样例:

    #include <stdio.h>
    
    
    #define MAX 18
    
    
    int
    main(int argc, char **argv)
    {
    	char name[MAX];
    	char *ptr;
    	printf("what't your name?
    ");
    	ptr = gets(name);		/*注意:gets()返回的指针与传递给它的是同一个指针*/
    	printf("%s?

    Ah ! %s ", ptr, name); return 0; }

    注意,假设gets出错时它返回一个空地址,因此在使用时一般使用例如以下技巧:

    while((ptr = gets(name)) != NULL){
    ……
    }


    2、gets函数的一个不足是它不检查预留存储区是否可以容纳实际输入的数据。多出来的字符简单地溢出到相邻的内存区。fgets()函数改进了这个问题。它可以指定最大读入字符数。fgets()和gets()有3点不同:

    a、它须要第二个參数来说明组大读入字符数。

    假设这个參数值为n,fgets()就会读取最多n-1个字符或者读完一个换行符为止。

    b、假设fgets读取到换行符。就会把它存放在字符串里。而不是像gets()那样丢弃它。也就是说它在字符串的最后存放的是换行符( ),而不是空字符()。

    c、它还须要第三个參数来说明读哪一个文件。

    #include <stdio.h>

    char *fgets(char *s, int size, FILE *strem);

    演示样例

    #include <stdio.h>
    
    #define MAX 18
    
    int
    main(int argc, char **argv)
    {
    	char name[MAX];
    	char *ptr;
    	printf("what't your name?

    "); ptr = fgets(name, MAX, stdin); printf("%s? Ah ! %s ", ptr, name); return 0; }

    编译測试结果显示:

    what't your name?
    libing
    libing
    ? Ah ! libing
    它显示了fgets()的一个不足之处,fgets()把换行符存储到字符串里了。这样每次显示字符串时就会显示换行符。


    以下对fgets()和gets()两个函数对于换行符不同处理的演示样例证明:

    #include <stdio.h>
    
    #define MAX 18
    
    int
    main(int argc, char **argv)
    {
    	char name1[MAX];
    	char name2[MAX];
    	char *ptr;
    	printf("what't your name?
    ");
    	ptr = fgets(name1, MAX, stdin);
    	ptr = gets(name2);
    	if(strcmp(name1, name2)){<span style="white-space:pre">	</span>/*比較两个字符串,由于gets会在最后将
    改为存入字符串,而fgets会直接将
    存入字符串*/
    		printf("name1 is not equal name2
    ");	
    	}
    	return 0;
    }

    3、scanf()和gets()基本的区别在于它们假设决定字符串何时结束。scanf()更给予获取单词而不是获取字符串;而gets()函数,会读取全部的字符,直到遇到第一个换行符为止。scanf()使用两种方法决定输入结束。假设使用%s格式。字符串读到下一个空白字符。假设指定了字段宽度,比方%10s,scanf()就会读入10个字符或直到遇到第一个空白字符。由二者中最先满足的哪一个终止输入。

    #include <stdio.h>
    
    int
    main(void)
    {
    	char name1[11], name2[11];
    	int count;
    
    	while(1){
    		printf("please input 2 names.
    ");	
    		count = scanf("%5s %10s", name1, name2);
    		printf("I read the %d names %s and %s.
    "
    						,count, name1, name2);
    
    	}
    	return 0;
    }
    执行执行測试:

    please input 2 names.
    Jesse Jukes
    I read the 2 names Jesse and Jukes.
    please input 2 names.
    Liza Applebottham
    I read the 2 names Liza and Applebotth.
    please input 2 names.
    Portensia Callowit
    I read the 2 names am and Portensia.
    please input 2 names.
    I read the 2 names Callo and wit.
    
    




  • 相关阅读:
    ubuntu下文件安装与卸载
    webkit中的JavaScriptCore部分
    ubuntu 显示文件夹中的隐藏文件
    C语言中的fscanf函数
    test
    Use SandCastle to generate help document automatically.
    XElement Getting OuterXML and InnerXML
    XUACompatible meta 用法
    Adobe Dreamweaver CS5.5 中文版 下载 注册码
    The Difference Between jQuery’s .bind(), .live(), and .delegate()
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/5141676.html
Copyright © 2011-2022 走看看