strtok_r 标记化后返回不正确的数据

strtok_r is returning incorrect data after tokenizing

下面是我尝试使用 strtok_r() 标记我的字符串以获取第一个标记的代码,即在本例中为 38。
如果您以某种方式观察到我的令牌被错误提取的输出(参考输出:38 被提取为 8)
这是一个间歇性问题,并非每次都会发生。我无法为此找到任何解决方案。

P.S。忽略我用来打印输出的记录器函数,它只是类似于 printf()

char cDelimeter = ',';
char *pRecordString = NULL;
char *pRemainString = NULL;
char szMsgBuff[MAX_BUFFER];

/*some code*/

LOG2(INF,"#ONLINEREQ : <%s>", szMsgBuff);
LOG3(INF,"#ONLINEREQ Before tokenizing: pRecordString<%s> pRemainString<%s>", pRecordString,pRemainString);
pRecordString = strtok_r(szMsgBuff,&cDelimeter,&pRemainString);
LOG3(INF,"#ONLINEREQ After tokenizing: pRecordString<%s> pRemainString<%s>", pRecordString,pRemainString);

输出

#ONLINEREQ : <38,CM|Member Name|Total ED|Net Buy Premium|Init Margin|ELMMargin|PreExpMargin|AdhocMargin|DeliveryMargin|UNDirectionalMRGN|CONMRGN|Crystallised MTM|Fwd Init Margin|Fwd Mntnce Margin|Total Utilization|Free Capital|MTM P/L|%ofEffectiveloss,^M
>
#ONLINEREQ Before tokenizing: pRecordString<(null)> pRemainString<(null)>
#ONLINEREQ After tokenizing: pRecordString<8> pRemainString<CM|Member Name|Total ED|Net Buy Premium|Init Margin|ELMMargin|PreExpMargin|AdhocMargin|DeliveryMargin|UNDirectionalMRGN|CONMRGN|Crystallised MTM|Fwd Init Margin|Fwd Mntnce Margin|Total Utilization|Free Capital|MTM P/L|%ofEffectiveloss,^M
>

根据手册页 strtok_r 有一个 const char * 分隔符。我猜你是通过指向一个随机终止的字符串来搞砸操作的,即 &cDelimeter.

尝试...

const char *cDelimeter = ",";

...

pRecordString = strtok_r(szMsgBuff,cDelimeter,&pRemainString);