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
galneweinhawAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.