zoukankan      html  css  js  c++  java
  • strtok和sscanf

    strtok------<string.h>

    功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
      
    说明:首次调用时,s必须指向要分解的字符串,随后调用要把s设成NULL。
            strtok在s中查找包含在delim中的字符并用NULL('\0')来替换,直到找遍整个字符串。
            返回指向下一个标记串。当没有标记串时则返回空字符NULL。

    Example

    /* STRTOK.C: In this program, a loop uses strtok
    * to print all the tokens (separated by commas
    * or blanks) in the string named "string".
    */
    #include <string.h>
    #include <stdio.h>
    char string[] = "A string\tof ,,tokens\nand some  more tokens";
    char seps[]   = " ,\t\n";
    char *token;
    void main( void )
    {
    printf( "%s\n\nTokens:\n", string );
    /* Establish string and get the first token: */
    token = strtok( string, seps );
    while( token != NULL )
    {
    /* While there are tokens in "string" */
    printf( " %s\n", token );
    /* Get next token: */
    token = strtok( NULL, seps );
    }
    }
    

    Output

    A string   of ,,tokens
    and some  more tokens
    Tokens:
    A
    string
    of
    tokens
    and
    some
    more
    tokens
    
     
     
    sscanf------------<string.h>
     
    #include <stdio.h>

    void main( void )
    {
       char  tokenstring[] = "15 12 14...";
       char  s[81];
       char  c;
       int   i;
       float fp;

       /* Input various data from tokenstring: */
       sscanf( tokenstring, "%s", s );
       sscanf( tokenstring, "%c", &c );
       sscanf( tokenstring, "%d", &i );
       sscanf( tokenstring, "%f", &fp );

       /* Output the data read */
       printf( "String    = %s\n", s );
       printf( "Character = %c\n", c );
       printf( "Integer:  = %d\n", i );
       printf( "Real:     = %f\n", fp );
    }

    Output

    String    = 15
    Character = 1
    Integer:  = 15
    Real:     = 15.000000
    
    zjfc-----1024
  • 相关阅读:
    数学工具WZgrapher
    零线和地线的区别,示波器如何测量市电?
    使用直流稳压电源时的注意事项!
    中文全角和半角输入有什么区别?
    ThinkingRock:使用方法
    2014记首
    如何使用Excel绘制甘特图
    AStyle代码格式工具在source insight中的使用
    STM32F103系列命名规则
    上市公司行情查询站点
  • 原文地址:https://www.cnblogs.com/pandy/p/1446999.html
Copyright © 2011-2022 走看看