在處理字符串的程序當中,經常會分析字符串,從一大長串的字符串截取我們需要的數據,這如果通過自己手寫函數來分析,雖然可以,但當你知道sscanf的功能,那就自己寫函數分析就顯得多此一舉。
這些函數的使用都很簡單,總結一下,等下次使用一目了然。俗話說:好記性不如爛筆頭,記錄下來就是效率。
以下源代碼是本人測試的源文件,附帶講解注釋。
- /*****************************************************
- ** Name : sscanf.c
- ** Author : gzshun
- ** Version : 1.0
- ** Date : 2011-12
- ** Description : sscanf function
- ******************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- static void sscanf_test(void);
-
- static void sscanf_test(void)
- {
- int ret;
- char *string;
- int digit;
- char buf1[255];
- char buf2[255];
- char buf3[255];
- char buf4[255];
-
- /*1.最簡單的用法*/
- string = "china beijing 123";
- 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.ret=2, buf1=china, buf2=beijing, digit=123
- **可以看出,sscanf的返回值是讀取的參數個數
- */
-
- /*2.取指定長度的字符串*/
- string = "123456789";
- sscanf(string, "%5s", buf1);
- printf("2.string=%s\n", string);
- printf("2.buf1=%s\n\n", buf1);
- /*
- **執行結果:
- **2.buf1=12345
- */
-
- /*3.取到指定字符為止的字符串*/
- string = "123/456";
- sscanf(string, "%[^/]", buf1);
- printf("3.string=%s\n", string);
- printf("3.buf1=%s\n\n", buf1);
- /*
- **執行結果:
- **3.buf1=123
- */
-
- /*4.取到指定字符集為止的字符串*/
- string = "123abcABC";
- sscanf(string, "%[^A-Z]", buf1);
- printf("4.string=%s\n", string);
- printf("4.buf1=%s\n\n", buf1);
- /*
- **執行結果:
- **4.buf1=123abc
- */
-
- /*5.取僅包含指定字符集的字符串*/
- 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.buf1=123
- */
-
- /*6.獲取指定字符中間的字符串*/
- 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:[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.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:[email protected]
- 9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
- */