user-0 提问: read读入数组后，怎样strstr一行一行的查找

回复:
user: user-0
content: 谢谢大家的帮忙，问题解决了！ 原因：返回结果中，有1个字符串中包含2个结果，用了if(strstr(x,x))... else if(strstr(x,x))...导致的，改为if(strstr(x,x))...if(strstr(x,x))...后正常了。

user: user-0
content: 引用 9 楼 zhao4zhong1 的回复:略改：#include <stdio.h> #include <string.h> #include <stdlib.h> const char * lpszBt = "<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><mem>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013-4-23 16:21:23</time></beatheart>"; char tszBuffer[1024]; char tszMatch[128]; int getmark(char *src,char *mark) {     char *p,*q;     int L;     char mk1[83],mk2[84];      sprintf(mk1,"<%.80s>",mark);     sprintf(mk2,"</%.80s>",mark);     p=strstr(src,mk1);     if (!p) return 1;     q=strstr(p,mk2);     if (!q) return 2;     L=strlen(mk1);     strncpy(tszMatch,p+L,__min(q-p-L,127));tszMatch[__min(q-p-L,127)]=0;     return 0; } int main() {     strncpy(tszBuffer,lpszBt,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"netcard")) return 1;     printf("%s\nnetcard=%s\n",tszBuffer,tszMatch);     strncpy(tszBuffer,tszMatch,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"device")) return 1;     printf("%s\ndevice=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"status")) return 1;     printf("%s\nstatus=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"usage")) return 1;     printf("%s\nusage=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"drop")) return 1;     printf("%s\ndrop=%s\n",tszBuffer,tszMatch);     return 0; } //<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><me //m>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013- //4-23 16:21:23</time></beatheart> //netcard=<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //device=eth0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //status=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //usage=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //drop=0   多谢了！

user: user-0
content: 引用 8 楼 zhao4zhong1 的回复:楼主换个跑马头像吧，看着眼晕

user: user-1
content: 1.遍历readbuf,统计换行符有几个,从而知道有多少行. 2.假设第1步统计出有N行, 则 int **arrLine = (int **)malloc(N * sizeof(int *));用于存放每行字符串的首地址 3.重新遍历readbuf,将每行字符串的首地址保存到arrLine, 同时将换行符改成字符串结束标记'\0' 4.使用arrLine遍历每一行, 调用strstr若查到关键字则返回. 若该关键字不止出现在一行,则事先malloc N个bool,遍历时将其设为true或false. 若不支持bool,可改用int,0表示假,1表示真.

user: user-0
content: read读入的数据包括空格、tab、回车等，有什麽办法过滤除想要的行？

user: user-2
content: 略改：#include <stdio.h> #include <string.h> #include <stdlib.h> const char * lpszBt = "<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><mem>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013-4-23 16:21:23</time></beatheart>"; char tszBuffer[1024]; char tszMatch[128]; int getmark(char *src,char *mark) {     char *p,*q;     int L;     char mk1[83],mk2[84];      sprintf(mk1,"<%.80s>",mark);     sprintf(mk2,"</%.80s>",mark);     p=strstr(src,mk1);     if (!p) return 1;     q=strstr(p,mk2);     if (!q) return 2;     L=strlen(mk1);     strncpy(tszMatch,p+L,__min(q-p-L,127));tszMatch[__min(q-p-L,127)]=0;     return 0; } int main() {     strncpy(tszBuffer,lpszBt,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"netcard")) return 1;     printf("%s\nnetcard=%s\n",tszBuffer,tszMatch);     strncpy(tszBuffer,tszMatch,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"device")) return 1;     printf("%s\ndevice=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"status")) return 1;     printf("%s\nstatus=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"usage")) return 1;     printf("%s\nusage=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"drop")) return 1;     printf("%s\ndrop=%s\n",tszBuffer,tszMatch);     return 0; } //<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><me //m>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013- //4-23 16:21:23</time></beatheart> //netcard=<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //device=eth0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //status=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //usage=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //drop=0

user: user-2
content: 楼主换个跑马头像吧，看着眼晕

user: user-0
content: 谢谢大家帮忙，问题解决了。 使用strstr返回的地址再过滤就行了，开始用整个readbuf内容来过滤了导致的。

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:仅供参考：#include <stdio.h> #include <string.h> #include <stdlib.h> const char * lpszBt = "<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><mem>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013-4-23 16:21:23</time></beatheart>"; char tszBuffer[1024]; char tszMatch[128]; char mk1[83],mk2[84]; int getmark(char *src,char *mark) {     char *p,*q;     int L;      sprintf(mk1,"<%.80s>",mark);     sprintf(mk2,"</%.80s>",mark);     p=strstr(tszBuffer,mk1);     if (!p) return 1;     q=strstr(p,mk2);     if (!q) return 2;     L=strlen(mk1);     strncpy(tszMatch,p+L,__min(q-p-L,127));tszMatch[__min(q-p-L,127)]=0;     return 0; } int main() {     strncpy(tszBuffer,lpszBt,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"netcard")) return 1;     printf("%s\nnetcard=%s\n",tszBuffer,tszMatch);     strncpy(tszBuffer,tszMatch,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"device")) return 1;     printf("%s\ndevice=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"status")) return 1;     printf("%s\nstatus=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"usage")) return 1;     printf("%s\nusage=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"drop")) return 1;     printf("%s\ndrop=%s\n",tszBuffer,tszMatch);     return 0; } //<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><me //m>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013- //4-23 16:21:23</time></beatheart> //netcard=<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //device=eth0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //status=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //usage=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //drop=0    谢谢！谢谢！

user: user-0
content: 引用 2 楼 ipqtjmqj 的回复:1.遍历readbuf,统计换行符有几个,从而知道有多少行. 2.假设第1步统计出有N行, 则 int **arrLine = (int **)malloc(N * sizeof(int *));用于存放每行字符串的首地址 3.重新遍历readbuf,将每行字符串的首地址保存到arrLine, 同时将换行符改成字符串结束标记'\0' 4.使用arrLine遍历每一行, 调用strstr若查到关键字则返回. 若该关键字不止出现在一行,则事先malloc N个bool,遍历时将其设为true或false. 若不支持bool,可改用int,0表示假,1表示真. 引用 2 楼 ipqtjmqj 的回复:1.遍历readbuf,统计换行符有几个,从而知道有多少行. 2.假设第1步统计出有N行, 则 int **arrLine = (int **)malloc(N * sizeof(int *));用于存放每行字符串的首地址 3.重新遍历readbuf,将每行字符串的首地址保存到arrLine, 同时将换行符改成字符串结束标记'\0' 4.使用arrLine遍历每一行, 调用strstr若查到关键字则返回. 若该关键字不止出现在一行,则事先malloc N个bool,遍历时将其设为true或false. 若不支持bool,可改用int,0表示假,1表示真.  非常感谢！每一步都给出来了。

user: user-2
content: 仅供参考：#include <stdio.h> #include <string.h> #include <stdlib.h> const char * lpszBt = "<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><mem>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013-4-23 16:21:23</time></beatheart>"; char tszBuffer[1024]; char tszMatch[128]; char mk1[83],mk2[84]; int getmark(char *src,char *mark) {     char *p,*q;     int L;      sprintf(mk1,"<%.80s>",mark);     sprintf(mk2,"</%.80s>",mark);     p=strstr(tszBuffer,mk1);     if (!p) return 1;     q=strstr(p,mk2);     if (!q) return 2;     L=strlen(mk1);     strncpy(tszMatch,p+L,__min(q-p-L,127));tszMatch[__min(q-p-L,127)]=0;     return 0; } int main() {     strncpy(tszBuffer,lpszBt,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"netcard")) return 1;     printf("%s\nnetcard=%s\n",tszBuffer,tszMatch);     strncpy(tszBuffer,tszMatch,1023);tszBuffer[1023]=0;     if (getmark(tszBuffer,"device")) return 1;     printf("%s\ndevice=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"status")) return 1;     printf("%s\nstatus=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"usage")) return 1;     printf("%s\nusage=%s\n",tszBuffer,tszMatch);     if (getmark(tszBuffer,"drop")) return 1;     printf("%s\ndrop=%s\n",tszBuffer,tszMatch);     return 0; } //<beatheart><probe_type>1</probe_type><probe_id>0800276D3191</probe_id><probe_ip>192.168.16.240</probe_ip><cpu>2</cpu><me //m>81</mem><hd>32</hd><netcard><device>eth0</device><status>0</status><usage>0</usage><drop>0</drop></netcard><time>2013- //4-23 16:21:23</time></beatheart> //netcard=<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //device=eth0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //status=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //usage=0 //<device>eth0</device><status>0</status><usage>0</usage><drop>0</drop> //drop=0

user: user-3
content: 字符串匹配  。