简体   繁体   English

为什么strtok_s在首次使用后返回0x00000001?

[英]Why does strtok_s return 0x00000001 after first use?

I am facing some issues while using strtok_s . 使用strtok_s一些问题。 In the below code, read_data contains the string: 在下面的代码中, read_data包含字符串:

BEGIN_TRANSACTION CHANGE_0 PUT(debit_account,dbvalue.new)PUT(debit_account,dbvalue.new) CHANGE_0 PUT(credit_account,crvalue.new)PUT(credit_account,crvalue.new) OUTCOME_COMMITTED_0 END_TRANSACTION_0

Below is my code: 下面是我的代码:

char *delim = " ";
char* next_token;
line[0] = strtok_s(read_data, delim, &next_token);
while (line[i] = strtok_s(NULL, delim, &next_token) != NULL)
{
    i++;
}

After execution, only line[0] contains correct string "BEGIN_TRANSACTION". 执行后,只有line[0]包含正确的字符串“ BEGIN_TRANSACTION”。 All the other line[i] 's contains "0x00000001 ". 其他所有line[i]包含“ 0x00000001”。 I have looked up online but I am unable to find a solution here. 我已经在网上查询,但无法在此处找到解决方案。 Any help is appreciated. 任何帮助表示赞赏。

Note: I am using Visual Studio 2010 注意:我正在使用Visual Studio 2010

while (line[i] = strtok_s(NULL, delim, &next_token) != NULL)

The above line should be as shown below. 上面的行应如下所示。 Note the brackets. 注意括号。

while ((line[i] = strtok_s(NULL, delim, &next_token)) != NULL)

To explain: The original while condition is not correct due to the operator order of precedence. 解释:由于操作员的优先顺序,原始的while条件不正确。 It equates to: 它等于:

line[i] = (strtok_s(NULL, delim, &next_token) != NULL)

Which means it takes the result of comparing the strtok_s return value to NULL and assigns that to line[i] . 这意味着它将strtok_s返回值与NULL进行比较并将结果分配给line[i] Which is why you get 1 for line[i] (except the last entry should be 0). 这就是为什么在line[i]得到1原因(除了最后一项应该为0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM