For the sake of learning, I'm trying to implement my own simple Tokenize function with CStrings. I currently have this file:
11111
22222
(ENDWPT)
222222
333333
(ENDWPT)
6060606
ggggggg
hhhhhhh
(ENDWPT)
iiiiiii
jjjjjjj
kkkkkkk
lllllll
mmmmmmm
nnnnnnn
Which I would like to be tokenized with the delimiter (ENDWPT). I coded the following function, which attempts to find the delimiter position, then add the delimiter length and extract the text to this position. After that, update a counter that is used so that the next time the function is called it begins searching for the delimiter from the previous index. The function looks like this:
bool MyTokenize(CString strText, CString& strOut, int& iCount)
{
CString strDelimiter = L"(ENDWPT)";
int iIndex = strText.Find(strDelimiter, iCount);
if (iIndex != -1)
{
iIndex += strDelimiter.GetLength();
strOut = strText.Mid(iCount, iIndex);
iCount = iIndex;
return true;
}
return false;
}
And is being called like so:
int nCount = 0;
while ((MyTokenize(strText, strToken, nCount)) == true)
{
// Handle tokenized strings here
}
Right now, the function is splitting the strings in the wrong way, I think it is because Find() may be returning the wrong index. I think it should be returning 12, but it is actually returning 14??.
I ran out of ideas, if anyone can figure this out I would really appreciate it.