|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//为什么最后输出的字符串前后都会多出来两个空格
// 目标是输出itcast字符串长度为6, 但是程序输出的字符串长度为10
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int trimSpace(char *inbuf, char *outbuf)
{
int i = 0, j = 0;
int ret = 0;
int count = 0;
char *tmp = inbuf;
if (inbuf == NULL || outbuf == NULL)
{
ret = -1;
printf("trimSpace() err %d\n", ret);
}
int len = strlen(inbuf);
j = len - 1;
if (tmp[i] == ' ' && tmp[i] != '\0')
{
++i;
}
if (tmp[j] == ' ' && j > 0)
{
--j;
}
count = j-i+1;
memcpy(outbuf, inbuf + i, count);
outbuf[count] = '\0';
return ret;
}
int getKeyByValude(char *keyvaluebuf, char *keybuf, char *valuebuf, int * valuebuflen)
{
int ret = 0;
char *p = NULL;
//int i = 0, j = 0;
char str[1024];
if (keyvaluebuf == NULL || keybuf == NULL || valuebuf == NULL || valuebuflen == NULL)
{
ret = -1;
printf("getKeyByValude() err %d\n", ret);
}
p = strstr(keyvaluebuf, keybuf);
if (p == NULL)
{
return 0;
}
p = p + strlen(keybuf);
p = strstr(keyvaluebuf, "=");
if (p == NULL)
{
return 0;
}
p = p + 1;
ret = trimSpace(p, str);
if (ret != 0)
{
ret = -2;
printf("trimSpace() err %d", ret);
}
strcpy(valuebuf, str);
*valuebuflen = strlen(str);
return ret;
}
int main()
{
int ret = 0;
char keyvaluebuf[] = {"ORCALE_name = itcast "};
char *keybuf = "ORCALE_name";
char valuebuf[1024];
int valuebuflen = 0;
ret = getKeyByValude(keyvaluebuf, keybuf, valuebuf, &valuebuflen);
if (ret != 0)
{
ret = -3;
printf("getKeyByValude() err %d", ret);
}
printf("%s\n", valuebuf);
printf("%d\n", valuebuflen);
return 0;
} |
|