printf gets fgets辨析

    技术2022-06-27  110

    一、辨析

     (1)scanf不能读包含有空格的字符串。

     (2)gets和fgets皆读一行内容,可含空格,gets不安全,尽量用fgets。

     (3)注意:若gets和fgets前有输入(如scanf),则要使 用getchar()后再使用gets或fgets,防止将前面的空格读入。

    二、facts/关键

     (1)明确各个函数对空格( ‘ ’)的处理。

     (2)想象有个指针指向终端或文件,依次后移。

    三、代码

       输入:长度<30的字符串,可包含空格

       输出:去掉空格后的字符串

    #include<stdio.h>

    #include<stdlib.h>

    #define MAX_CHAR_NUM 30

    void cut_string(char * p1,char * p2)

    {

         int i=0;

         int j=0;

         while(p1[i]!='/0')

         {

           if (p1[i]!=' ')

            {

               p2[j]=p1[i];

               j++;

            }

           i++;

         }

         p2[j]='/0';

    }

    int main()

    {

      char a1[MAX_CHAR_NUM];

      char a2[MAX_CHAR_NUM];                              // (1)allocate memory(data struct)

      printf("please input the string:/n");

      fgets(a1,MAX_CHAR_NUM+1,stdin);                 // (2)initialize original data

      cut_string(a1,a2);                                            // (3)operate 

      printf("%s/n",a2);                                            // (4)print result

    }

     

    #if 0     //test_code,incomplete

    int main()

    {

      int num_char;

      char *p1;

      char *p2;

      scanf("%d",&num_char);

      printf("num_char is:%d/n",num_char);

      p1=(char*)malloc(sizeof(char)*(num_char+1));

      p2=(char*)malloc(sizeof(char)*(num_char+1));

     getchar();

      fgets(p1,num_char+1,stdin);

      printf("%s/n",p1);

      cut_string2(num_char,p1,p2);

     printf("%s",p2);

    }

    #endif

    另转

    http://linux.about.com/library/cmd/blcmdl3_fgets.htm

    NAME

    fgetc, fgets, getc, getchar, gets, ungetc - input of characters and strings    

    SYNOPSIS

    #include <stdio.h > int fgetc(FILE * stream ); char *fgets(char * s , int size , FILE * stream ); int getc(FILE * stream ); int getchar(void); char *gets(char * s ); int ungetc(int c , FILE * stream );  

    DESCRIPTION

    fgetc()   reads the next character from   stream  and returns it as an   unsigned char   cast to an int , or   EOF   on end of file or error.

    getc()   is equivalent to  fgetc()   except that it may be implemented as a macro which evaluatesstream   more than once.

    getchar()   is equivalent to  getc( stdin ) .

    gets()   reads a line from  stdin   into the buffer pointed to by  s   until either a terminating newline or  EOF , which it replaces with  '/0' . No check for buffer overrun is performed (see  BUGS   below).

    fgets()   reads in at most one less than  size   characters from  stream   and stores them into the buffer pointed to by  s . Reading stops after an  EOF   or a newline. If a newline is read, it is stored into the buffer. A  '/0'   is stored after the last character in the buffer.

    ungetc()   pushes  c   back to  stream , cast to  unsigned char , where it is available for subsequent read operations. Pushed - back characters will be returned in reverse order; only one pushback is guaranteed.

    Calls to the functions described here can be mixed with each other and with calls to other input functions from the  stdio   library for the same input stream.

    For non-locking counterparts, see  unlocked_stdio (3).   

    RETURN VALUE

    fgetc() ,   getc()  and   getchar()   return the character read as an   unsigned char   cast to an   int or   EOF   on end of file or error.

    gets()   and  fgets()   return  s   on success, and  NULL   on error or when end of file occurs while no characters have been read.

    ungetc()   returns  c   on success, or  EOF   on error.   

    CONFORMING TO

    ANSI - C, POSIX.1    

    SEE ALSO

    read (2),   write (2),   ferror (3),   fopen (3),   fread (3),   fseek (3),   puts (3),   scanf (3), unlocked_stdio (3)

     

     

     


    最新回复(0)