gpt4 book ai didi

c - 带分隔符的Strtok行为

转载 作者:IT王子 更新时间:2023-10-29 01:05:46 24 4
gpt4 key购买 nike

下面是代码片段。

#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 %s\n", sensor_number, event_catagory, sensor_type, event_state, assertion, message_number, short_text);
}

这很好用。但是,当“行”参数是“前板内存状态:检测到可纠正的 ECC/其他可纠正的内存错误时;传感器(70,内存)”

输出将是

70 SENSOR_SPECIFIC MEMORY STATE_00 True 8543 前板内存状态

其中 short_text 变量仅包含“前板内存状态”而不是“前板内存状态:检测到可纠正的 ECC/其他可纠正的内存错误;传感器(70,内存)”

为什么 strtok 将单个冒号视为分隔符?谁能解决这个问题。

最佳答案

Why strtok considering a single colon as delimiter?

因为在标准(C11)中有规定:

7.24.5.8 The strtok function

[...]

  1. A sequence of calls to the strtok function breaks the string pointed to by s1 into asequence of tokens, each of which is delimited by a character from the string pointed toby s2. The first call in the sequence has a non-null first argument; subsequent calls in thesequence have a null first argument. The separator string pointed to by s2 may bedifferent from call to call.

关于c - 带分隔符的Strtok行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29843047/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com