1

strtok_r像这样使用:

char *the_sting = "a|b||e|f";
char *last;
char *current;

current = (char*)strtok_r(the_sting, "|", &last);

while(current != NULL)
{
    printf(current);
    printf("\n");
    current = (char*)strtok_r(NULL, "|", &last);
}

我得到:

>>a
>>b
>>e
>>f

问题是,当分隔符之间没有任何内容时,我需要“空白”。

喜欢:

>>a
>>b
>>
>>e
>>f
4

5 回答 5

1

然后strtok_r不是你的函数,此外,你不能使用字符串文字char *the_sting = "a|b||e|f";,因为strtok_r修改了这样的字符串,而是使用数组char the_sting[] = "a|b||e|f";

最后,不要printf以这种方式使用printf(current);(很危险),而是:

printf("%s", current);

这个小功能可以满足您的要求:

#include <stdio.h>
#include <string.h>

char *scan(char **pp, char c)
{
    char *s, *p;

    p = strchr(*pp, c);
    if (p) *p++ = '\0';
    s = *pp;
    *pp = p;
    return s;
}

int main(void)
{
    char the_sting[] = "a|b||e|f"; /* I think you mean the_string here */
    char *s, *p = the_sting;

    while (p) {
        s = scan(&p, '|');
        printf("<%s>", s);
    }
    return 0;
}

请注意,使用简单char(不是字符串)作为分隔符

于 2013-05-23T12:04:01.700 回答
1

比较当前current与前一个current。如果差值大于strlen(previous_current) + 1则跳过一个或多个空位。

于 2013-05-23T11:19:27.220 回答
0

自己动手很容易,真的:

#include <string.h>

typedef struct {
    const unsigned char *data;
    size_t len;
} buffer_t;

/* Use strpbrk() for multiple delimiters. */    
buffer_t
memtok(const void *s, size_t length, const char *delim, buffer_t *save_ptr)
{
    const unsigned char *stream,
                        *token;
    size_t len = 0;

    if (NULL == s) {
        stream = save_ptr->data;
    } else {
        stream = s;
        save_ptr->len = length;
    }

    token = stream;

    /* Advance until either a token is found or the stream exhausted. */
    while (save_ptr->len--) {
        if (memchr(delim, *stream, strlen(delim))) {
            /* Point save_ptr past the (non-existent) token. */
            save_ptr->data = stream + 1;
            return (buffer_t) { .data = token, .len = len };
        }

        ++len;
        ++stream;
    }

    /* State : done. */
    *save_ptr = (buffer_t) { .data = NULL, .len = 0 };

    /* Stream exhausted but no delimiters terminate it. */
    return (buffer_t){ .data = token, .len = len };
}

并进行简短测试:

int main(int argc, char **argv)
{
    const char *the_sting = "a|b||e|f";
    buffer_t kek = { .data = the_sting, .len = 8 },
             token, state;

    token = memtok(the_sting, 8, "|", &state);

    while (token.data != NULL) {
        char test[512];

        memcpy(test, token.data, token.len);
        test[token.len] = 0;
        printf("%s\n", test);

        token = memtok(NULL, 0, "|", &state);
    }

    return 0;
}
于 2013-05-23T11:58:21.940 回答
0

这个怎么样:

char s[] = "1,2,,,,,,,3,4,5,6";
char *tok, *saved;
tok = strtok_r(s, ",", &saved);
do
{
    fprintf(stderr, "tok = %s, saved = %s\n", tok, saved);;
    if (',' == *saved)
    {
        while (',' == *saved++ )
        {
            fprintf(stderr, "saved = %s\n", saved);;
        }
        *saved--;
    }
} while( (tok = (strtok_r(((void *)0), ",", &saved))));
于 2014-05-14T11:35:32.780 回答
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *my_strtok_r(char *str, const char *delims, char **store){
    char *p, *wk;
    if(str != NULL){
        *store = str;
    }
    if(*store == NULL) return NULL;
    //*store += strspn(*store, delims);//skip delimiter
    if(**store == '\0') return NULL;
    p=strpbrk(wk=*store, delims);
    if(p != NULL){
        *p='\0';
        *store = p + 1;
    } else {
        *store = NULL;
    }
    return wk;
}


int main(void){
    char the_sting[] = "a|b||e|f";
    char *last;
    char *current;

    current = my_strtok_r(the_sting, "|", &last);

    while(current != NULL)
    {
        printf(current);
        printf("\n");
        current = my_strtok_r(NULL, "|", &last);
    }
    return 0;    
}
于 2013-05-23T11:25:00.810 回答