c++字符串问题求助
//为什么最后输出的字符串前后都会多出来两个空格// 目标是输出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 == ' ' && tmp != '\0')
{
++i;
}
if (tmp == ' ' && j > 0)
{
--j;
}
count = j-i+1;
memcpy(outbuf, inbuf + i, count);
outbuf = '\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;
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;
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;
} 这个函数你改下
int trimSpace(char *inbuf, char *outbuf)
{
int ret = 0;
int len = strlen(inbuf);
char *pch = outbuf;
for (int iloop = 0; iloop < len;iloop++){
if (' ' != inbuf){
*pch = inbuf;
pch++;
}
}
return ret;
}
迷雾少年 发表于 2015-8-18 08:21
这个函数你改下
int trimSpace(char *inbuf, char *outbuf)
{
谢谢
页:
[1]