AnswerSeeker
AnswerSeeker

Reputation: 203

c++ CString Tokenize strange issue

I observed a strange issue but am not able to figure out why this is happening. Any inputs on this greatly appreciated.

Here is my code:

CString strValue;
strValue = "99\tStop\t";

CString strToken;
int pos = 2;
strToken = strValue.Tokenize(_T("\t"), pos);

cout << strToken;

This will return me "Stop" which is correct (please note the line has a tab separator for each entry)

However, for an input

strValue = "100\tStart\t"

The strToken returned is "0".

Any ideas about this?

Upvotes: 0

Views: 552

Answers (1)

luk32
luk32

Reputation: 16080

Err... ok. I think what you observe is expected. After reading this Tokenize.

Esp this part : CStringT Tokenize( PCXSTR pszTokens, int& iStart ) const; [...] "On each call to Tokenize the function starts at iStart, skips leading delimiters, and returns a CStringT object containing the current token, which is the string of characters up to the next delimiter character."

You start at position 2.

"99  Stop    "
"100  Start  "
 012  <-- pos

In the 1st case for pos = 2 you start at \t and ignore all the leading delimiters and returns the string till the next one, which is Stop. In the 2nd case, you start at 0, and the next character is a specified delimiter, thus you get string from pos = 2 till \t, it is only one character 0.

Mystery solved.

Upvotes: 3

Related Questions