Below is the code where I am trying to use strtok_r() to tokenize my string to fetch the first token i.e. 38 in this case.
If you observe the output somehow my token is incorrectly fetched (refer output :38 is fetched as 8)
This is an intermittent issue which doesn't occur everytime. I am unable to find any solution for this.
P.S. Ignore the logger function I used to print output, it's just something similar to 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);
Output
#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
>
According to the man page strtok_r
has a const char *
delimeter. I would guess that you're screwing up the operation by pointing to a randomly terminated string, i.e.&cDelimeter
.
Try ...
const char *cDelimeter = ",";
...
pRecordString = strtok_r(szMsgBuff,cDelimeter,&pRemainString);