首页 > 代码库 > sscanf sprintf sstream

sscanf sprintf sstream

  1 #include <stdio.h>  
  2 #include <stdlib.h>  
  3 #include <string.h>  
  4   
  5 static void sscanf_test(void);  
  6   
  7 static void sscanf_test(void)  
  8 {  
  9     int ret;  
 10     char *string;  
 11     int  digit;  
 12     char buf1[255];  
 13     char buf2[255];  
 14     char buf3[255];  
 15     char buf4[255];  
 16   
 17     /*1.最简单的用法*/  
 18     string = "china beijing 123";  
 19     ret = sscanf(string, "%s %s %d", buf1, buf2, &digit);  
 20     printf("1.string=%s\n", string);  
 21     printf("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, buf1, buf2, digit);  
 22     /* 
 23     **执行结果: 
 24     **1.ret=3, buf1=china, buf2=beijing, digit=123 
 25     **可以看出,sscanf的返回值是读取的参数个数 
 26     */  
 27   
 28     /*2.取指定长度的字符串*/  
 29     string = "123456789";  
 30     sscanf(string, "%5s", buf1);  
 31     printf("2.string=%s\n", string);  
 32     printf("2.buf1=%s\n\n", buf1);  
 33     /* 
 34     **执行结果: 
 35     **2.buf1=12345 
 36     */  
 37   
 38     /*3.取到指定字符为止的字符串*/  
 39     string = "123/456";  
 40     sscanf(string, "%[^/]", buf1);  
 41     printf("3.string=%s\n", string);  
 42     printf("3.buf1=%s\n\n", buf1);  
 43     /* 
 44     **执行结果: 
 45     **3.buf1=123 
 46     */  
 47   
 48     /*4.取到指定字符集为止的字符串*/  
 49     string = "123abcABC";  
 50     sscanf(string, "%[^A-Z]", buf1);  
 51     printf("4.string=%s\n", string);  
 52     printf("4.buf1=%s\n\n", buf1);  
 53     /* 
 54     **执行结果: 
 55     **4.buf1=123abc 
 56     */  
 57   
 58     /*5.取仅包含指定字符集的字符串*/  
 59     string = "0123abcABC";  
 60     sscanf(string, "%[0-9]%[a-z]%[A-Z]", buf1, buf2, buf3);  
 61     printf("5.string=%s\n", string);  
 62     printf("5.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);  
 63     /* 
 64     **执行结果: 
 65     **5.buf1=123, buf2=abc, buf3=ABC 
 66     */  
 67   
 68     /*6.获取指定字符中间的字符串*/  
 69     string = "ios<android>wp7";  
 70     sscanf(string, "%*[^<]<%[^>]", buf1);  
 71     printf("6.string=%s\n", string);  
 72     printf("6.buf1=%s\n\n", buf1);  
 73     /* 
 74     **执行结果: 
 75     **6.buf1=android 
 76     */  
 77   
 78     /*7.指定要跳过的字符串*/  
 79     string = "iosVSandroid";  
 80     sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);  
 81     printf("7.string=%s\n", string);  
 82     printf("7.buf1=%s, buf2=%s\n\n", buf1, buf2);  
 83     /* 
 84     **执行结果: 
 85     **7.buf1=ios, buf2=android 
 86     */  
 87   
 88     /*8.分割以某字符隔开的字符串*/  
 89     string = "android-iphone-wp7";  
 90     /* 
 91     **字符串取道‘-‘为止,后面还需要跟着分隔符‘-‘, 
 92     **起到过滤作用,有点类似于第7点 
 93     */  
 94     sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);  
 95     printf("8.string=%s\n", string);  
 96     printf("8.buf1=%s, buf2=%s, buf3=%s\n\n", buf1, buf2, buf3);  
 97     /* 
 98     **执行结果: 
 99     **8.buf1=android, buf2=iphone, buf3=wp7 
100     */  
101   
102     /*9.提取邮箱地址*/  
103     string = "Email:beijing@sina.com.cn";  
104     sscanf(string, "%[^:]:%[^@]@%[^.].%s", buf1, buf2, buf3, buf4);  
105     printf("9.string=%s\n", string);  
106     printf("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", buf1, buf2, buf3, buf4);  
107     /* 
108     **执行结果: 
109     **9.buf1=Email, buf2=beijing, buf3=sina, buf4=com.cn 
110     */  
111   
112     /*10.过滤掉不想截取或不需要的字符串--补充, 
113     **在%号后面加一*号,代表过滤这个字符串,不读取 
114     */  
115     string = "android iphone wp7";  
116     sscanf(string, "%s %*s %s", buf1, buf2);  
117     printf("10.string=%s\n", string);  
118     printf("10.buf1=%s, buf2=%s\n\n", buf1, buf2);  
119     /* 
120     **执行结果: 
121     **10.android wp7 
122     */  
123 }  
124   
125 int main(int argc, char **argv)  
126 {  
127     sscanf_test();  
128       
129     return 0;  
130 }  

sprintf功能也很强   是把数据写进字符串里面   不多赘述

 

sstream是C++才有的

用流处理  也非常强大

 

不过鉴于不准备以后继续学习C++了 

实在太浪费时间还难找工作

下段时间主要就注重算法

开始学习java 和 python  

sscanf sprintf sstream