首页 > 代码库 > sscanf、strsep

sscanf、strsep

#include <stdio.h>#include <string.h>int main(){    char token[] ="abdzxbcdefgh";    char str[]="3:2:09";    int a1=0, a2=0, a3=0;    int ret;    printf("%s\n",token);    char *tokenremain = token;    char *tok1 = strsep(&tokenremain,"cde");    printf("tok1:%s,token:%s\n",tok1,tokenremain);    tok1 = strsep(&tokenremain,"cde");                                               printf("tok1:%s,token:%s\n",tok1,tokenremain);    printf("%s\n", token );    ret=sscanf( str, "%d:%d:%d", &a1, &a2, &a3 );    printf( "ret=%d,%d:%d:%d\n", ret, a1, a2, a3 );    return 0;}

输出:

abdzxbcdefgh
tok1:ab,token:zxbcdefgh
tok1:zxb,token:defgh
ab
ret=3,3:2:9

 

注意问题:

1、sscanf返回值,是识别token的个数;

2、strsep会修改被扫描的字符串,会在扫描到的地方设置为‘\0‘

 

sscanf、strsep