- 论坛徽章:
- 14
|
看了楼主的代码,首先结果就是错误的,8个a,但只输出了前7个a。这是个小错误
龌龊的是 memset( szStr,0x00,sizeof(szStr) ) 这一句,根本就没有必要。任何寄希望用memset来消除错误,而不去仔细查找错误所在的行为,常常会恰得其反。虽然你这里幸运地没出现错误。- #include <stdio.h>
- //#include <stdlib.h>
- #include <string.h>
- int main(void)
- {
- char *pchStr = "<10001>aaaaaaaa</10001>";
- char *pchStart = NULL;
- char *pchEnd = NULL;
- char szStr[1024+1];
- // memset( szStr,0x00,sizeof(szStr) );
- pchStart = strstr(pchStr,"<10001>")+strlen("<10001>");
- //pchEnd = strstr(pchStr,"</10001>")-1;
- pchEnd = strstr(pchStr,"</10001>");
- memcpy( szStr,pchStart,pchEnd - pchStart );
- szStr[pchEnd-pchStart] = '\0';
- printf("%s\n",szStr);
- return 0;
- }
复制代码- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- const char *pchStr = "<10001>aaaaaaaa</10001>";
- const char *pchStart = NULL;
- const char *pchEnd = NULL;
- pchStart = strstr(pchStr,"<10001>");
- if( pchStart )
- {
- pchStart += strlen("<10001>");
- pchEnd = strstr(pchStart,"</10001>");
- if( pchEnd )
- {
- printf("%.*s\n",pchEnd-pchStart,pchStart);
- }
- }
- return 0;
- }
复制代码 |
|