C: Parse empty tokens from a string with strtok

后端 未结 8 1138
臣服心动
臣服心动 2020-12-10 19:08

My application produces strings like the one below. I need to parse values between the separator into individual values.

2342|2sd45|dswer|2342||5523|||3654|         


        
8条回答
  •  没有蜡笔的小新
    2020-12-10 19:53

    Use something other than strtok. It's simply not intended to do what you're asking for. When I've needed this, I usually used strcspn or strpbrk and handled the rest of the tokeninzing myself. If you don't mind it modifying the input string like strtok, it should be pretty simple. At least right off, something like this seems as if it should work:

    // Warning: untested code. Should really use something with a less-ugly interface.
    char *tokenize(char *input, char const *delim) { 
        static char *current;    // just as ugly as strtok!
        char *pos, *ret;
        if (input != NULL)
            current = input;
    
        if (current == NULL)
            return current;
    
        ret = current;
        pos = strpbrk(current, delim);
        if (pos == NULL) 
            current = NULL;
        else {
            *pos = '\0';
            current = pos+1;
        }
        return ret;
    }
    

提交回复
热议问题