Convert CString to char*

Posted on 2007-10-10
Last Modified: 2013-11-20
How to convert CString to char *
Question by:xtran888
    LVL 45

    Accepted Solution

    Hi xtran888,

    If all you want to do is get to the underlying string (and perhaps modify it) use the GetBuffer() method.  Here's a quick example.

    CString s(_T("File.ext"));
    LPTSTR p = s.GetBuffer();
    LPTSTR dot = strchr(p, '.'); // OK, should have used s.Find...
    if(p != NULL)
        *p = _T('\0');

    Good Luck,
    LVL 4

    Expert Comment

    If you only need read-only access to the string, you can cast the CString to LPCSTR, if your environment is setup for ANSI (multi-byte) strings.

    CString sTest("test");
    const char *p = (LPCSTR)sTest;

    If your build environment is set for Unicode, you'll get a type cast error because then the CString will compile to Unicode, and so you would need to add code to convert the string at runtime.
    LVL 39

    Expert Comment

    >>>> const char *p = (LPCSTR)sTest;

    CString has a built-in cast operator for LPCSTR (== const char*). So you need no cast:

        CString s = "Hello";
        const char* psz = s;  // no cast needed

    That means wherever a const char* (LPCSTR) was used you may use a CString instead.

    Regards, Alex

    LVL 39

    Expert Comment

    >>> If your build environment is set for Unicode, you'll get a
    >>> type cast error because then the CString will compile to Unicode
    Using the TCHAR and LPCTSTR will solve that either for UNICODE or non-UNICODE.

      CString s = _T("Hello");   // makes either LPCSTR or LPCWSTR on right side
      LPTSTR p = new TCHAR[20];   // left side is either char* or wchar_t*
                                                     // TCHAR is either char or wchar_t
      _tcscpy(p, s);   // here you don't need to cast to LPCTSTR as it is built-in
                              // _tcscopy makes strcpy in case of non-UNICODE and wcscpy for UNICODE

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
    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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    733 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

    20 Experts available now in Live!

    Get 1:1 Help Now