string = "ios<Android>wp7"; sscanf(string, "%*[^<]<%[^>]", buf1); printf("6.string=%s\n", string); printf("6.buf1=%s\n\n", buf1); /* **执行结果: **6.buf1=android */ /*7.指定要跳过的字符串*/ 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.buf1=ios, buf2=android */ /*8.分割以某字符隔开的字符串*/ 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.buf1=android, buf2=iphone, buf3=wp7 */ /*9.提取邮箱地址*/ string = "Email:beijing@sina.com.cn"; 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.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn */ } int main(int argc, char **argv) { sscanf_test(); return 0; } /* **测试程序 **环境: **Linux Ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux **gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ** gzshun@ubuntu:~/c/sscanf$ gcc sscanf.c -o sscanf gzshun@ubuntu:~/c/sscanf$ ./sscanf 1.string=china beijing 123 1.ret=3, buf1=china, buf2=beijing, digit=123 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:beijing@sina.com.cn 9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn 上一页 [1] [2]
|