问题描述:获取字符串中两个字符之间的数据。最近写SMTP代理,需要获取发送方与接收方的邮件名称。
字符串为 “MAIL FROM:<test1@163.com>\r\n” , 获取字符':'冒号和字符'\r'之间的值 <test1@163.com>
/*C获取字符串中两个字符之间的的数据*/
#include <stdio.h>
#include <string.h>
#define SUCCESS 0
int test(char *pcBuf, char *pcRes)
{
char *pcBegin = NULL;
char *pcEnd = NULL;
pcBegin = strstr(pcBuf, ":");
pcEnd = strstr(pcBuf, "\r");
if(pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd)
{
printf("Mail name not found!\n");
}
else
{
pcBegin += strlen(":");
memcpy(pcRes, pcBegin, pcEnd-pcBegin);
}
return SUCCESS;
}
int main()
{
char cBuf[40960] = {"MAIL FROM:<test1@163.com>\r\n"};
char cRes[40960] = {0};
test(cBuf, cRes);
printf("%s\n", cRes);
return SUCCESS;
}
[root@localhost test]# ./strncmp
<test1@163.com>