zoukankan      html  css  js  c++  java
  • sscanf函数

    读取格式化的字符串中的数据

    int sscanf(const char *str, const char *format, ...);

    format:
    *:跳过此数据不读
    %[^a]:匹配到非a字符,停止
    %[a-z] :匹配a到z中任意字符

    跳过一个字符串不读

    char *str = "0 123";
    char buf[20] = {0};
    sscanf(str, "%*s%s", buf);
    puts(buf);
    
    # ./a.out      
    123

    指定长度

    char *str = "12345";
    char buf[20] = {0};
    sscanf(str, "%3s", buf);
    puts(buf);
    
    # ./a.out      
    123

    固定字符以前的字符串

    char *str = "0:123";
    char buf[20] = {0};
    sscanf(str, "%[^:]", buf);
    puts(buf);
    
    # ./a.out      
    0

    固定字符以后的数据

    char *str = "0:123";
    char buf[20] = {0};
    sscanf(str, "%*[^:]:%s", buf);
    puts(buf);
    
    # ./a.out      
    123

    完全匹配指定字符

    char *str = "12345";
    char buf[20] = {0};
    sscanf(str, "%[1-3]", buf);
    puts(buf);
    
    # ./a.out      
    123
  • 相关阅读:
    项目管理原则
    开发规范
    讲故事-如何才算确认了需求
    关于概要设计
    jQuery操作
    IE8,IE9,IE10绿色版,以及ColorPix
    机务UI设计小节
    Abstract Factory
    Flyweight
    Chain of Responsibility
  • 原文地址:https://www.cnblogs.com/zhangxuechao/p/11709693.html
Copyright © 2011-2022 走看看