在处理字符串的程序当中,经常会分析字符串,从一大长串的字符串截取我们需要的数据,这如果通过自己手写函数来分析,虽然可以,但当你知道sscanf的功能,那就自己写函数分析就显得多此一举。

这些函数的使用都很简单,总结一下,等下次使用一目了然。俗话说:好记性不如烂笔头,记录下来就是效率。

  1 /*****************************************************
  2 ** Name         : sscanf.c 
  3 ** Author       : gzshun
  4 ** Version      : 1.0
  5 ** Date         : 2011-12
  6 ** Description  : sscanf function
  7 ******************************************************/
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <string.h>
 11 
 12 static void sscanf_test(void);
 13 
 14 static void sscanf_test(void)
 15 {
 16     int ret;
 17     char *string;
 18     int  digit;
 19     char buf1[255];
 20     char buf2[255];
 21     char buf3[255];
 22     char buf4[255];
 23 
 24     /*1.最简单的用法*/
 25     string = "china beijing 123";
 26     ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);
 27     printf("1.string=%s\n", string);
 28     printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit);
 29     /*
 30     **执行结果:
 31     **1.ret=3, buf1=china, buf2=beijing, digit=123
 32     **可以看出,sscanf的返回值是读取的参数个数
 33     */
 34 
 35     /*2.取指定长度的字符串*/
 36     string = "123456789";
 37     sscanf(string, "%5s", buf1);
 38     printf("2.string=%s\n", string);
 39     printf("2.buf1=%s\n\n", buf1);
 40     /*
 41     **执行结果:
 42     **2.buf1=12345
 43     */
 44 
 45     /*3.取到指定字符为止的字符串*/
 46     string = "123/456";
 47     sscanf(string, "%[^/]", buf1);
 48     printf("3.string=%s\n", string);
 49     printf("3.buf1=%s\n\n", buf1);
 50     /*
 51     **执行结果:
 52     **3.buf1=123
 53     */
 54 
 55     /*4.取到指定字符集为止的字符串*/
 56     string = "123abcABC";
 57     sscanf(string, "%[^A-Z]", buf1);
 58     printf("4.string=%s\n", string);
 59     printf("4.buf1=%s\n\n", buf1);
 60     /*
 61     **执行结果:
 62     **4.buf1=123abc
 63     */
 64 
 65     /*5.取仅包含指定字符集的字符串*/
 66     string = "0123abcABC";
 67     sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);
 68     printf("5.string=%s\n", string);
 69     printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);
 70     /*
 71     **执行结果:
 72     **5.buf1=123, buf2=abc, buf3=ABC
 73     */
 74 
 75     /*6.获取指定字符中间的字符串*/
 76     string = "ios<android>wp7";
 77     sscanf(string, "%*[^<]<%[^>]", buf1);
 78     printf("6.string=%s\n", string);
 79     printf("6.buf1=%s\n\n", buf1);
 80     /*
 81     **执行结果:
 82     **6.buf1=android
 83     */
 84 
 85     /*7.指定要跳过的字符串*/
 86     string = "iosVSandroid";
 87     sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
 88     printf("7.string=%s\n", string);
 89     printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2);
 90     /*
 91     **执行结果:
 92     **7.buf1=ios, buf2=android
 93     */
 94 
 95     /*8.分割以某字符隔开的字符串*/
 96     string = "android-iphone-wp7";
 97     /*
 98     **字符串取道\'-\'为止,后面还需要跟着分隔符\'-\',
 99     **起到过滤作用,有点类似于第7点
100     */
101     sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
102     printf("8.string=%s\n", string);
103     printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);
104     /*
105     **执行结果:
106     **8.buf1=android, buf2=iphone, buf3=wp7
107     */
108 
109     /*9.提取邮箱地址*/
110     string = "Email:beijing@sina.com.cn";
111     sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4);
112     printf("9.string=%s\n", string);
113     printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4);
114     /*
115     **执行结果:
116     **9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
117     */
118 
119     /*10.过滤掉不想截取或不需要的字符串--补充,
120     **在%号后面加一*号,代表过滤这个字符串,不读取
121     */
122     string = "android iphone wp7";
123     sscanf(string, "%s %*s %s", buf1, buf2);
124     printf("10.string=%s\n", string);
125     printf("10.buf1=%s, buf2=%s\n\n", buf1, buf2);
126     /*
127     **执行结果:
128     **10.android wp7
129     */
130 }
131 
132 int main(int argc, char **argv)
133 {
134     sscanf_test();
135 
136     return 0;
137 }
138 
139 /*
140 **测试程序
141 **环境:
142 **Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 i686 GNU/Linux
143 **gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
144 **
145 gzshun@ubuntu:~/c/sscanf$ gcc sscanf.c -o sscanf
146 gzshun@ubuntu:~/c/sscanf$ ./sscanf
147 1.string=china beijing 123
148 1.ret=3, buf1=china, buf2=beijing, digit=123
149 2.string=123456789
150 2.buf1=12345
151 3.string=123/456
152 3.buf1=123
153 4.string=123abcABC
154 4.buf1=123abc
155 5.string=0123abcABC
156 5.buf1=0123, buf2=abc, buf3=ABC
157 6.string=ios<android>wp7
158 6.buf1=android
159 7.string=iosVSandroid
160 7.buf1=ios, buf2=android
161 8.string=android-iphone-wp7
162 8.buf1=android, buf2=iphone, buf3=wp7
163 9.string=Email:beijing@sina.com.cn
164 9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn
165 10.string=android iphone wp7
166 10.buf1=android, buf2=wp7
167 */

 

版权声明:本文为wangshaowei原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/wangshaowei/p/10837263.html