Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to convert CString to char*?

How to convert CString to char*?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
8 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello, Can anyone tell me how to convert CString to char*? Thanks!

    C D P L W 5 Replies Last reply
    0
    • N nachilau

      Hello, Can anyone tell me how to convert CString to char*? Thanks!

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      did you try (char *)csMyCString ? -c


      To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
         /. #3848917

      Fractals!

      N 1 Reply Last reply
      0
      • C Chris Losinger

        did you try (char *)csMyCString ? -c


        To explain Donald Knuth's relevance to computing is like explaining Paul's relevance to the Catholic Church. He isn't God, he isn't the Son of God, but he was sent by God to explain God to the masses.
           /. #3848917

        Fractals!

        N Offline
        N Offline
        Navin
        wrote on last edited by
        #3

        It depends - do you want a constant char *? Then doing as Chris said works fine (and usually happens automatically.) If you want a char * that's not constant, try CString::GetBuffer (and CString::ReleaseBuffer when you're done with the char *.) Good ol' strcpy to a char array also works. :) Even if you win the rat race, you're still a rat.

        1 Reply Last reply
        0
        • N nachilau

          Hello, Can anyone tell me how to convert CString to char*? Thanks!

          D Offline
          D Offline
          Dave Bryant
          wrote on last edited by
          #4

          If you only need a constant string, you can use the implicit LPCTSTR operator of CString, which under ANSI builds will give you a const char*. If you need a non-constant string, then you can use the GetBuffer() method to get direct access to the underlying buffer, and then ReleaseBuffer() when you have finished with it. Dave

          1 Reply Last reply
          0
          • N nachilau

            Hello, Can anyone tell me how to convert CString to char*? Thanks!

            P Offline
            P Offline
            paulb
            wrote on last edited by
            #5

            You can also do... (LPTSTR) (LPCTSTR) strYourString it's a bit dangerous though but can be useful when you need a LPTSTR and know it won't ever be modified.

            1 Reply Last reply
            0
            • N nachilau

              Hello, Can anyone tell me how to convert CString to char*? Thanks!

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yeah: 1) CString csTemp = "bla bla"; char *szResult; szResult = new char[csTemp.GetLength() + 1]; strcpy(szResult,csTemp); or csTemp.GetBuffer(0); //This returns char*

              V 1 Reply Last reply
              0
              • L Lost User

                Yeah: 1) CString csTemp = "bla bla"; char *szResult; szResult = new char[csTemp.GetLength() + 1]; strcpy(szResult,csTemp); or csTemp.GetBuffer(0); //This returns char*

                V Offline
                V Offline
                vikramlinux
                wrote on last edited by
                #7

                in MSDN there is one example given Example The following example demonstrates the use of CString::operator LPCSTR. // If the prototype of a function is known to the compiler, // the LPCTSTR cast operator may be invoked implicitly CString strSports(_T("Hockey is Best!")); TCHAR sz[1024]; lstrcpy(sz, strSports); // If the prototype isn't known, or is a va_arg prototype, // you must invoke the cast operator explicitly. For example, // the va_arg part of a call to sprintf() needs the cast: sprintf(sz, "I think that %s!\n", (LPCTSTR) strSports); // while the format parameter is known to be an LPCTSTR and // therefore doesn't need the cast: sprintf(sz, strSports); // Note that some situations are ambiguous. This line will // put the address of the strSports object to stdout: cout << strSports; // while this line will put the content of the string out: cout << (LPCTSTR) strSports;

                1 Reply Last reply
                0
                • N nachilau

                  Hello, Can anyone tell me how to convert CString to char*? Thanks!

                  W Offline
                  W Offline
                  Williams
                  wrote on last edited by
                  #8

                  Hi, Try GetBuffer methode of CString. e.g char *t; CString str = "teste"; t = str.GetBuffer(10); :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups