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
posted @ 2017-05-22 16:53  thomas_blog  阅读(97)  评论(0编辑  收藏  举报