• 关于setbuf()缓存的设置 fgets()读入一行字符串


    /*输入:2
      12345
      abcde
      希望输出:
      0:12345
      1:abcde
    */
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main( void )
    {
            char **instr, **p;
            int i, j;
     
            printf( "num: " );
            scanf( "%d", &j );//不能有"\n"
            if( !j )
                 return 0;
    //fflush( stdin );       // 论坛上讲用fflush()  或 setbuf()都能达到同样的结果,但我用fflush()没有实现.
    setbuf(stdin,NULL);
            instr = (char **)malloc( sizeof(char *) * j );
            if( !instr )
                   return 1;
            memset( instr, 0, sizeof(char *) * j );
            for( i = 0, p = instr; i < j; i++ )
            {
                   printf( "i=%d:", i );
                   *p = (char *)malloc( sizeof(char) * 80 );
                   if( !*p )
                          return 1;
                  fgets( *p,80,stdin );//从标准输入读入一行字符串,包括回车换行符"\n"
                    (*p)[strlen(*p)-1] = '\0';
                    *p++;
            }
     
            for( i = 0, p = instr; i < j; i++ )
            {
                    printf( "%d: %s\n", i, *p );
                    free( *p++ );
            }
            free( instr );
            return 0;
    }
  • 相关阅读:
    20170519
    20170515
    20170511
    20170509
    20170505
    使用 FirewallD 构建动态防火墙
    Elasticsearch 5.2.1Cluster 搭建
    elk,centos7,filebeat,elasticsearch-head详细安装步骤
    elk,centos7,filebeat,elasticsearch-head集成搭建
    memcached
  • 原文地址:https://www.cnblogs.com/daniel/p/52546.html
Copyright © 2020-2023  润新知