1 #include2 #include 3 #include 4 5 int main(){ 6 char str[100]; 7 //用法一:取指定长度的字符串 8 sscanf("12345","%4s",str); 9 printf("用法一\nstr = %s\n",str);10 11 //用法二:格式化时间12 int year,month,day,hour,minute,second;13 sscanf("2013/02/13 14:55:34","%d/%d/%d %d:%d:%d",&year,&month,&day,&hour,&minute,&second);14 printf("用法二\ntime = %d-%d-%d %d:%d:%d\n",year,month,day,hour,minute,second);15 16 //用法三:读入字符串17 sscanf("12345","%s",str);18 printf("用法三\nstr = %s\n",str);19 20 //用法四:%*d 和 %*s 加了星号 (*) 表示跳过此数据不读入. (也就是不把此数据读入参数中)21 sscanf("12345acc","%*d%s",str);22 printf("用法四\nstr = %s\n",str);23 24 //用法五:取到指定字符为止的字符串。如在下例中,取遇到'+'为止字符串。25 sscanf("12345+acc","%[^+]",str);26 printf("用法五\nstr = %s\n",str);27 28 //用法六:取到指定字符集为止的字符串。如在下例中,取遇到小写字母为止的字符串。29 sscanf("12345+acc121","%[^a-z]",str);30 printf("用法六\nstr = %s\n",str);31 return 0;32 }
本文转自:http://blog.csdn.net/sjf0115/article/details/8579935