• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1906
  • Last Modified:

How to use a CString with strtok() function

I want to use the strtok() function but the parameter I need to provide is a CString:

strtok(thisIsCString,"*")

I get the error:
"cannot convert parameter 1 from 'class CString' to 'char *'"

how do I convert my CString to a char* ???

thanks
0
galneweinhaw
Asked:
galneweinhaw
1 Solution
 
jkrCommented:
>>how do I convert my CString to a char* ???

It has the 'operator LPCTSTR()' which would return a const char* - however, since 'strtok()' manipulates the char* it is working on, I'd suggest using

char* pTmp = strdup((LPCTSTR)thisIsCString);

strtok(pTmp,"*");

//...

free(pTmp);
0
 
Jaime OlivaresSoftware ArchitectCommented:
Just have to obtain a pointer to the string before using strtok:


const char *str = (LPCTSTR)thisIsCString;

0
 
jkrCommented:
>>const char *str = (LPCTSTR)thisIsCString;

There's a reason why the 1st argument to 'strtok()' is *not* const. For any other purpose but that however, you're right.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, since strtok is defined as:
char *strtok( char *strToken, const char *strDelimit );
Will be better to use a non-constant:

char *str = thisIsCString.GetBuffer(0);
Then you can use:
strtok(str ,"*")

0
 
AxterCommented:
FYI:
>>char *str = thisIsCString.GetBuffer(0);

If you use this method, make sure to call ReleaseBuffer() member function before using any other CString member functions.

char *str = thisIsCString.GetBuffer(0);
Then you can use:
strtok(str ,"*")
//after done with str buffer

thisIsCString.ReleaseBuffer();
0
 
galneweinhawAuthor Commented:
jkr,

thanks for the help and I appreciate your explanation as well, it helps to understand what's going on.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now