user-0 提问: sscanf语句读取 字符串信息

回复:
user: user-0
content: 我自己用strstr查找status format_address字符串，后再sscanf，解决了。 这样做能够比较准确的定位到指定的字符串  程序代码： /*********************************sscanf匹配长字符串函数*******************************/ /************************************************************************************/ /************************************************************************************/ /************************************************************************************/ /************************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <string.h>  char data_buff[] = "<GeocoderSearchResponse>\r\n\t<status>0</status>\r\n\tsomething\r\n\t<formatted_address>geocode chinese address here</formatted_address>\r\n</GeocoderSearchResponse>\r\n"; // \t代表table int main(void)  {          int status;         char geo_addr[100];         int ret;                  char *p; printf("data_buff :\n%s\n",data_buff); system("pause");   p = strstr(data_buff,"<status>"); if(p == NULL)     {         printf("<status> Not Found!>");         return (-1);     } printf("p = %s\n",p); ret = sscanf(p,"<status>%d</status>",&status); printf("ret from html: %d\n",ret); printf("status from html: %d\n",status); system("pause");  p = strstr(data_buff,"<formatted_address>"); if(p == NULL)     {         printf("<formatted_address> Not Found!>");         return (-1);     } printf("p = %s\n",p); ret = sscanf(p,"<formatted_address>%[^<]</formatted_address>",geo_addr); printf("ret from html: %d\n",ret); printf("geo_addr from html: %s\n",geo_addr);  return(0);  }

user: user-1
content: #include <stdio.h> #include <string.h> #include <stdlib.h> char data_buff[] = "<GeocoderSearchResponse>\r\n" "   <status>0</status>\r\n" "   something" "   <formatted_address>geocode chinese address here</formatted_address>\r\n" "</GeocoderSearchResponse>\r\n"; int main() {     int status;     char geo_addr[100];     int ret;      ret=sscanf(data_buff, "<GeocoderSearchResponse>" "%*[^<]<status>%d</status>" "%*s" "%*[^<]<formatted_address>%99[^<]" ,&status,geo_addr);     if (2==ret) {         printf("status from html: %d\n",status);         printf("geo_addr from html: [%s]\n",geo_addr);         return 0;     } else {         printf("Format error:[%s]\n",data_buff);         return 1;     } } //status from html: 0 //geo_addr from html: [geocode chinese address here] //

user: user-1
content: #include <stdio.h> #include <string.h> #include <stdlib.h> char data_buff[] = "<GeocoderSearchResponse>\r\n" "   <status>0</status>\r\n" "   something" "   <formatted_address>geocode chinese address here</formatted_address>\r\n" "</GeocoderSearchResponse>\r\n"; char *p,*q; int L; int main() {     int status;     char geo_addr[100];     int ret;      p=strstr(data_buff,"<status>");     if (NULL==p) goto ERR;     ret = sscanf(p+strlen("<status>"),"%d",&status);     if (ret!=1) goto ERR;      p=strstr(data_buff,"<formatted_address>");     if (NULL==p) goto ERR;     q=strstr(data_buff,"</formatted_address>");     if (NULL==q || q<p) goto ERR;     L=__min(100-1,q-p-strlen("<formatted_address>"));     strncpy(geo_addr,p+strlen("<formatted_address>"),L);geo_addr[L]=0;     printf("status from html: %d\n",status);     printf("geo_addr from html: [%s]\n",geo_addr);     return 0; ERR:     printf("Format error:[%s]\n",data_buff);     return 1; } //status from html: 0 //geo_addr from html: [geocode chinese address here] //