|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
int spitString(const char *mytmp, char c, char ***mybuf, int *num)
{
int cls = 0;
if (mytmp == NULL || num == NULL)
{
cls = -1;
printf("func spitString() err:%d (mytmp == NULL || num == NULL) \n", cls);
goto END;
}
char *p1 = mytmp;
int count = 0;
while (p1 = strchr(p1, ','))
{
count++;
p1++;
}
char **pp = NULL;
pp = (char **)malloc(count * sizeof(char *));
if (pp == NULL)
{
cls = -2;
printf("func spitString() err:%d count * sizeof(char *)\n", cls);
goto END;
}
char *p = NULL, *ptmp = NULL;
count = 0;
p = mytmp;
ptmp = mytmp;
int len = p - ptmp + 1;
do
{
p = strchr(p, c);
if (p != NULL)
{
if (p - ptmp > 0)
{
pp[count] = (char *)malloc(len * sizeof(char));
if (pp[count] == NULL)
{
cls = -3;
printf("func spitString() err:%d (char *)malloc(len * sizeof(char))", cls);
goto END;
}
strncpy(pp[count], ptmp, p - ptmp);
pp[count][p - ptmp] = '\0';
count++;
ptmp = p = p + 1;
}
}
else
{
break;
}
} while (*p!='\0');
END:
if (cls != 0)
{
if (pp == NULL)
{
return -4;
}
Free_String(&pp, count);
}
else
{
*num = count;
*mybuf = pp;
}
return cls;
}
void Free_String(char ***mybuf, int num)
{
if (mybuf == NULL)
{
return;
}
int i;
char **tmp = NULL;
tmp = *mybuf;
if (tmp == NULL)
{
return;
}
for (i = 0; i < num; i++)
{
if (tmp[i] != NULL)
{
free(tmp[i]);
}
}
if (tmp != NULL)
{
free(tmp);
}
*mybuf = NULL;
}
int main()
{
int cls = 0;
char tmp[100] = { "abcdef,acccd,eeee,aaaa,e3eeee,ssss," };
char c = ',';
int num = 0, i;
char **buf = NULL;
cls = spitString(tmp, c, &buf, &num);
if (cls != 0)
{
printf("fucn spitString() err:%d \n", cls);
return cls;
}
for (i = 0; i < num; i++)
printf("%s \n", buf[i]);
printf("%d \n", num);
for (i = 0; i < num; i++)
{
if (buf[i] != NULL)
{
free(buf[i]);
buf[i] = NULL;
}
}
if (buf != NULL)
{
free(buf);
buf = NULL;
}
//Free_String(&buf, num);
return cls;
}
改了好多地方
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Free_String(char **mybuf, int num)
{
if(mybuf == NULL)
return;
for(int i = 0; i < num; ++i) {
free(mybuf[i]);
}
free(mybuf);
}
int spitString(const char *mytmp, char c, char ***mybuf, int *num)
{
if(mytmp == NULL || num == NULL)
return -1;
const char *p1 = mytmp;
int count = 0;
while(p1 = strchr(p1, ','))
{
count++;
p1++;
}
++count;
char **pp = NULL;
pp = malloc(count * sizeof(char *));
if(pp == NULL)
{
*mybuf = pp;
return -2;
}
const char *p = mytmp, *q;
for(int i = 0; i < count; ++i)
pp[i] = NULL;
for(int i = 0; i < count; ++i) {
q = strchr(p, c);
if(!q) q = p + strlen(p);
pp[i] = malloc(q - p + 1);
if(!pp[i]) return -3;
strncpy(pp[i], p, q - p);
pp[i][q - p] = '\0';
p = q + 1;
}
*mybuf = pp;
*num = count;
return 0;
}
int main(void)
{
int cls = 0;
char tmp[100] = {"abcdef,acccd,eeee,aaaa,e3eeee,ssssa,"};
char c = ',';
int num = 0, i;
char **buf = NULL;
cls = spitString(tmp, c, &buf, &num);
if(cls != 0)
{
printf("fucn spitString() err: %d\n", cls);
return cls;
}
for(i = 0; i < num; i++)
printf("%s\n", buf[i]);
printf("%d\n", num);
Free_String(buf, num);
return cls;
}
|
|