Strtok delimited behavior

Below is a code snippet.

#define TABLE_DELIMITER "::"
int parse_n_store ( char *line )

        int i = 0;
        char *p = NULL;
        CPTR sensor_number = NULL , event_catagory = NULL, sensor_type = NULL, event_state= NULL, assertion = NULL, message_number = NULL, short_text = NULL;

        for (p = strtok(line,TABLE_DELIMITER); p != NULL; p = strtok(NULL, TABLE_DELIMITER), i++ )
                if ( i == 0 )
                else if ( i == 1 )
                        sensor_type = p;
                else if ( i == 2 )
                        event_catagory = p;
                else if ( i == 3 )
                        event_state = p;
                else if ( i == 4 )
                        assertion = p;
                else if ( i == 5 )
                        message_number = p;
                else if ( i == 6 )
                        short_text = p;

        printf ("%s %s %s %s %s %s %s\n", sensor_number, event_catagory, sensor_type, event_state, assertion, message_number, short_text);


This works great. But, when the argument "string" is "Front Panel Memory Status: Recoverable ECC Patch / Other Patch Error Detected, Sensor (70, Memory)"

The output will be

70 SENSOR_SPECIFIC MEMORY STATE_00 True 8543 Front panel memory status

where the variable short_text contains only "Front panel memory status" instead of "Front panel memory status: detected Fixed ECC patch / other correctable memory error, sensor (70, Memory)"

Why does strtok treat a single colon as a separator? Can anyone solve this problem.


source to share

2 answers

Why does strtok treat a single colon as a separator?

Because it is specified in the standard (C11): strtok function


  1. The sequence of calls to the strtok function interrupts the string pointed to by s1 into a sequence of tokens, each delimited by a character from the string pointed to by s2. The first call in the sequence has a nonzero first argument; subsequent calls to Sequence have a zero first argument. The delimiter string pointed to by s2 can be different from call to call.


You can try strstr

to iterate through a string as it can search for a substring.

You can define (beware of unverified):

char *strmtok(char *s, char *delim) {
    static char *current = NULL;
    char *ix, *cr;

    if (s != NULL) { 
        current = s;
    ix = strstr(current, delim);
    if (ix == NULL) return NULL;
    cr = current;
    current = ix + strlen(delim);
    *ix = '\0';
    return cr;


and use that as a replacement for the original strtok.



All Articles