Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to use a CString with strtok() function

Posted on 2004-10-26
Medium Priority
Last Modified: 2012-06-27
I want to use the strtok() function but the parameter I need to provide is a CString:


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

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

Question by:galneweinhaw
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 12412976
>>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);



LVL 55

Expert Comment

by:Jaime Olivares
ID: 12412988
Just have to obtain a pointer to the string before using strtok:

const char *str = (LPCTSTR)thisIsCString;

LVL 86

Expert Comment

ID: 12413029
>>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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 12413032
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 ,"*")

LVL 30

Expert Comment

ID: 12413210
>>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


Author Comment

ID: 12413563

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question