Solved

How to use a CString with strtok() function

Posted on 2004-10-26
1,816 Views
Last Modified: 2012-06-27
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
Question by:galneweinhaw
    6 Comments
     
    LVL 86

    Accepted Solution

    by:
    >>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
     
    LVL 55

    Expert Comment

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


    const char *str = (LPCTSTR)thisIsCString;

    0
     
    LVL 86

    Expert Comment

    by:jkr
    >>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
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    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
     
    LVL 30

    Expert Comment

    by:Axter
    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
     

    Author Comment

    by:galneweinhaw
    jkr,

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    877 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now