Below is the 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 ) sensor_number=p; 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 %sn", sensor_number, event_catagory, sensor_type, event_state, assertion, message_number, short_text); }
This works fine. But, when the “line” argument is “Front Board Memory status:Correctable ECC / other correctable memory error detected ; sensor (70, Memory)”
The output will be
70 SENSOR_SPECIFIC MEMORY STATE_00 True 8543 Front Board Memory status
where the short_text variable contains only “Front Board Memory status” instead of “Front Board Memory status:Correctable ECC / other correctable memory error detected ; sensor (70, Memory)”
Why strtok considering a single colon as delimiter? Can anyone solve this issue.
Advertisement
Answer
Why strtok considering a single colon as delimiter?
Because it is specified in the standard(C11):
7.24.5.8 The strtok function
[…]
- A sequence of calls to the strtok function breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has a non-null first argument; subsequent calls in the sequence have a null first argument. The separator string pointed to by s2 may be different from call to call.