现有字符串"clamscan c:\Documents and Settings d:\test",要把它分成
串1:"clamscan" 串2"c:\Documents and Settings" 串3"d:\test"
当然,是不能数字符串个数截取的,因为c:\Documents and Settings或d:\test是任意的路径。
现在我已经写好了一个函数,能按空格分割,可是那样的话c:\Documents and Settings 就分成了c:\Documents 和and 和 Settings三部分,求各位仗义大侠相救。。。
谢谢Helicopter的关注,情况是这样的
c:\Documents and Settings clamscan d:\test 这种情况是不会存在的,现在主要是解决后边路径的分割问题,当然后边的路径不一定是两个,可以没有,也可以是多个
要达到这样的效果,用sscanf看来是搞不定了,只能自己写函数。
下面的函数看是否满足需求,你可以在全局变量str[]里加任意多的路径,但有一点注意的是,字符\在串里必须转义为\\,路径长度不超过128位即可。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char str[] = "clamscan c:\\Documents and Settings d:\\test e:\\a and b f:\\c and d";
void main()
{
int i = 0, j = 0;
char (*pStr)[128] = NULL;
char *s = str;
char *ss = NULL;
while (1)
{
pStr = (char (*)[128])realloc(pStr, (i+1)*(128));
memset(*(pStr+i), 0, 128);
ss = strchr(s, ':');
if (i == 0)
{
if (ss == NULL)
{
strcpy(*(pStr+i), s);
i++;
break;
}
memcpy(*(pStr+i), s, ss-s-2);
s = ss+1;
i++;
continue;
}
if (ss == NULL)
{
strcpy(*(pStr+i), s-2);
i++;
break;
}
memcpy(*(pStr+i), s-2, ss-s);
s = ss+1;
i++;
}
printf("After despare, the strings are:\n");
for (j = 0; j < i; j++)
{
printf("[%s]\n", *(pStr+j));
}
free(pStr);
}
输出效果: