"Default User" <de***********@yahoo.com> wrote in news:3lpjfkF13p0arU1
@individual.net:
nobody wrote:
Now I feel dumb and it works like a charm.
The thing that doesn't make sense is that I made a function calling
strtok() initially. ex:
getField("hkml\\software","\\");
then getField calls strtok(), so literal passed through another
variable doesn't make that variable modifiable (ie: strtok can't
modify it)?
Arrays that are passed to functions are not copied. Instead, the array
name is converted to a pointer to the first element. It is undefined
behavior to modify a string literal, such as when strtok() punches
holes in the string as it works down through it.
You'd be much better off with std::string and using one of the various
techniques for doing something similar to strtok() that yield a vector
of strings. Some have been posted here in the past.
Brian
Since the first reply I moved away from strtok because it's easier to
just do the samething myself (hard to believe myself:)).
Can someone tell me if my code is optimized and what could I do better
(areas of improvement)? In my code I let the caller release the memory
(someone said that's the COM way). Is it better to use a CONST for the
first argument?
//return left side of the delimiter
TCHAR* _tleft(TCHAR* lpszString, CONST TCHAR* chDelimiter)
{
BYTE byteSize = _tcsclen(lpszString) + 1;
TCHAR *lpszCurPos;
lpszCurPos = lpszString;
TCHAR *lpszFound = 0;
TCHAR *lpszOut = new TCHAR[byteSize];
TCHAR *lpszOut2;
lpszOut2 = lpszOut; //hold final string
lpszFound = _tcsstr(lpszString,chDelimiter);
while ((*lpszCurPos)&&((lpszFound - lpszCurPos) != 0))
{
*lpszOut++ = (TCHAR)*lpszCurPos++;
}
*lpszOut='\0';
return lpszOut2;
}
//return right side of the delimiter
TCHAR* _tright(TCHAR* lpszString, CONST TCHAR* chDelimiter)
{
BYTE byteSize = _tcsclen(lpszString) + 1;
TCHAR *lpszCurPos;
lpszCurPos = lpszString;
TCHAR *lpszFound = 0;
TCHAR *lpszOut = new TCHAR[byteSize];
TCHAR *lpszOut2;
lpszOut2 = lpszOut; //hold final string
lpszFound = _tcsstr(lpszString,chDelimiter);
if(lpszFound)
{
while ((*lpszCurPos++)&&((lpszFound - lpszCurPos) != 0))
{}
while(*lpszCurPos++)
{
*lpszOut++ = (TCHAR)*lpszCurPos;
}
*lpszOut='\0';
return lpszOut2;
}
else
return NULL;
}