#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; void sscanf_test(void) { int ret; char *string; int digit; char buf1[255]; char buf2[255]; char buf3[255]; char buf4[255]; /*1.最简单的用法*/ cout<<"======================================"<<endl; string = "UESTC EE 4200802"; ret = sscanf(string, "%s %s %d", buf1, buf2, &digit); printf("1.string=%s\n", string); printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit); /*********************************************** 1.string=UESTC EE 4200802 1.ret=3, buf1=UESTC, buf2=EE, digit=4200802 ***************************************************/ /*2.取指定长度的字符串*/ cout<<"======================================"<<endl; string = "123456789"; sscanf(string, "%5s", buf1); printf("2.string=%s\n", string); printf("2.buf1=%s\n\n", buf1); /**************************************** 2.string=123456789 2.buf1=12345 ***************************************/ /*3.取到指定字符为止的字符串*/ cout<<"======================================"<<endl; string = "123/456"; sscanf(string, "%[^/]", buf1); printf("3.string=%s\n", string); printf("3.buf1=%s\n\n", buf1); /******************************************** 3.string=123/456 3.buf1=123 ********************************************/ /*4.取到指定字符集为止的字符串*/ cout<<"======================================"<<endl; string = "123abcABC"; sscanf(string, "%[^A-Z]", buf1); printf("4.string=%s\n", string); printf("4.buf1=%s\n\n", buf1); /*********************************** 4.string=123abcABC 4.buf1=123abc ************************************/ /*5.取仅包含指定字符集的字符串*/ cout<<"======================================"<<endl; string = "0123abcABC"; sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3); printf("5.string=%s\n", string); printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); /******************************************* 5.string=0123abcABC 5.buf1=0123, buf2=abc, buf3=ABC *******************************************/ /*6.获取指定字符中间的字符串*/ cout<<"======================================"<<endl; string = "ios<Android>wp7"; sscanf(string, "%*[^<]<%[^>]", buf1); printf("6.string=%s\n", string); printf("6.buf1=%s\n\n", buf1); /************************************* 6.string=ios<Android>wp7 6.buf1=Android ***************************************/ /*7.指定要跳过的字符串*/ cout<<"======================================"<<endl; string = "iosVSandroid"; sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2); printf("7.string=%s\n", string); printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2); /******************************* 7.string=iosVSandroid 7.buf1=ios, buf2=android ************************************/ /*8.分割以某字符隔开的字符串*/ cout<<"======================================"<<endl; string = "android-iphone-wp7"; /************************************************ **字符串取道'-'为止,后面还需要跟着分隔符'-', **起到过滤作用,有点类似于第7点 *************************************************/ sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3); printf("8.string=%s\n", string); printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3); /*********************************************** 8.string=android-iphone-wp7 8.buf1=android, buf2=iphone, buf3=wp7 **************************************************/ /*9.提取邮箱地址*/ cout<<"======================================"<<endl; string = "Email:[email protected]"; sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4); printf("9.string=%s\n", string); printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4); /************************************** 9.string=Email:[email protected] 9.buf1=Email, buf2=shihui512, buf3=139, buf4=com *************************************/ } int main(int argc, char **argv) { sscanf_test(); return 0; } /****************************************** ====================================== 1.string=UESTC EE 4200802 1.ret=3, buf1=UESTC, buf2=EE, digit=4200802 ====================================== 2.string=123456789 2.buf1=12345 ====================================== 3.string=123/456 3.buf1=123 ====================================== 4.string=123abcABC 4.buf1=123abc ====================================== 5.string=0123abcABC 5.buf1=0123, buf2=abc, buf3=ABC ====================================== 6.string=ios<Android>wp7 6.buf1=Android ====================================== 7.string=iosVSandroid 7.buf1=ios, buf2=android ====================================== 8.string=android-iphone-wp7 8.buf1=android, buf2=iphone, buf3=wp7 ====================================== 9.string=Email:[email protected] 9.buf1=Email, buf2=shihui512, buf3=139, buf4=com Process returned 0 (0x0) execution time : 0.109 s Press any key to continue. *******************************************/