今日快报
sscanf_的简单用法你知道吗?
2022-03-21 01:50  浏览:238

描述:

定义函数 int sscanf (const char *str,const char * format,........);
从一个字符串中读进与指定格式相符得数据。

有关format符号上得特殊用法

  • %[^a]: 表示取到指定字符为止得字符串,a表示具体字符
  • 取仅包含指定字符集得字符串,数字和小写字母

    %[1-9] 表示匹配1到9中得任意数字

    %[a-z] 表示匹配a到z小写字母中得任意字符

  • 取到指定字符集为止得字符串

    %[^A-Z] 遇到大写字母为止得字符串

  • %*[]: 可用于格式中,(即%*d和%*s) 加了星号(*) 表示跳过此数据不读入。(也就是不把此数据读入到参数中)

    ex1:char str[100] = {0};sscanf("cncr/09CNCRFF等122", "%*[^/]%s", str)=> str = /09CNCRFF等122

    ex2:char str[100] = {0};sscanf("cncr/09CNCRFF等122", "%*[^/]/%[^等]", str)=> str = 09CNCRFF

    ex3:char str[100] = {0};sscanf("cncr/09CNCRFF等122", "%*[^/]%[^等]", str);=> str = /09CNCRFF

    ex4:char str[100] = {0};sscanf("hello world", "%*s%s", str);=> str = world