平行于PHP的"爆炸"在C:斯普利特的char *转换成char *使用分隔符 [英] Parallel to PHP's "explode" in C: Split char* into char* using delimiter

查看:202
本文介绍了平行于PHP的"爆炸"在C:斯普利特的char *转换成char *使用分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  将字符串分割在C 分隔符

我在寻找一个好办法,爆炸一个char *到其他的char *使用的分隔符。

I'm searching a good way to "explode" a char* into other char* using a delimiter.

我的分隔符将

推荐答案

您可以使用 strtok的像CrazyCasta说,但他/她的code是错误的。

You can use strtok like CrazyCasta said but his/hers code is wrong.

char *tok;
char *src = malloc(strlen(srcStr) + 1);
memcpy(src, srcStr);

tok = strtok(src, "#");
if(tok == NULL)
{
    printf("no tokens found");
    free(src);
    return ???;
}
printf("%s ; ", tok);
while((tok = strtok(NULL, "#")))
    printf("%s ; ", tok);
printf("\n");
free(str);

请注意, strtok的国际上被称为第一次与源指针,在那之后你必须使用 NULL 。此外的src 必须是可写的,因为 strtok的写入 \\ 0 来终止出现的字符串。因此,这取决于你如何读取字符串(是否你要算账或不使用它),你应该做它的一个副本。但正如我所说,这并不总是必要的。

Be aware that strtok has to be called the first time with the source pointer, after that you have to use NULL. Also src must be writeable because strtok writes \0 to terminate the found strings. Hence, depending on how you read the string (and whether you are going to use it afterwards or not), you should do a copy of it. But as I said, this is not always necessary.

编辑:

爆炸函数可能看起来像这样:

an explode function could look like this:

char *strdup(const char *src)
{
    char *tmp = malloc(strlen(src) + 1);
    if(tmp)
        strcpy(tmp, src);
    return tmp;
}

void explode(const char *src, const char *tokens, char ***list, size_t *len)
{   
    if(src == NULL || list == NULL || len == NULL)
        return;

    char *str, *copy, **_list = NULL, **tmp;
    *list = NULL;
    *len  = 0;

    copy = strdup(src);
    if(copy == NULL)
        return;

    str = strtok(copy, tokens);
    if(str == NULL)
        goto free_and_exit;

    _list = realloc(NULL, sizeof *_list);
    if(_list == NULL)
        goto free_and_exit;

    _list[*len] = strdup(str);
    if(_list[*len] == NULL)
        goto free_and_exit;
    (*len)++;


    while((str = strtok(NULL, tokens)))
    {   
        tmp = realloc(_list, (sizeof *_list) * (*len + 1));
        if(tmp == NULL)
            goto free_and_exit;

        _list = tmp;

        _list[*len] = strdup(str);
        if(_list[*len] == NULL)
            goto free_and_exit;
        (*len)++;
    }


free_and_exit:
    *list = _list;
    free(copy);
}

那么你必须调用它:

then you have to call it:

char **list;
size_t i, len;
explode("this;is;a;string", ";", &list, &len);
for(i = 0; i < len; ++i)
    printf("%d: %s\n", i+1, list[i]);

/* free list */
for(i = 0; i < len; ++i)
    free(list[i]);
free(list);

这是Valgrind的运行例如:

this is an example running with valgrind:

valgrind ./a 
==18675== Memcheck, a memory error detector
==18675== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al. 
==18675== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==18675== Command: ./a 
==18675== 
1: this
2: is
3: a
4: string
==18675== 
==18675== HEAP SUMMARY:
==18675==     in use at exit: 0 bytes in 0 blocks
==18675==   total heap usage: 9 allocs, 9 frees, 114 bytes allocated
==18675== 
==18675== All heap blocks were freed -- no leaks are possible
==18675== 
==18675== For counts of detected and suppressed errors, rerun with: -v
==18675== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)

这篇关于平行于PHP的&QUOT;爆炸&QUOT;在C:斯普利特的char *转换成char *使用分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆