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. Converting CString to char*

Converting CString to char*

Scheduled Pinned Locked Moved C / C++ / MFC
c++
20 Posts 7 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.
  • D DSPCottage

    Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");

    Z Offline
    Z Offline
    zafersavas
    wrote on last edited by
    #3

    There a various ways: 1)

    CString str = "filename";

    char charPtr[100];
    sprintf(charPtr, "%s", str);

    CString str = "filename";
    char* charPtr = str.GetBuffer(str.GetLength());

    S 1 Reply Last reply
    0
    • S super_ttd

      It should do. So, what's wrong ? Do you get an error ? CString has an inner cast Operator (LPCTSTR) which converts the CString object to a const char* (if compiling ANSI) of const wchar_t* (if building unicode), so you should not have to do any much stuff. And please (if you get this advice from anywhere), don't use the CString::GetBuffer()) method at all for such a thing !!! So, to come back to you problem, what is your problem ?


      Z Offline
      Z Offline
      zafersavas
      wrote on last edited by
      #4

      Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)

      M C 2 Replies Last reply
      0
      • D DSPCottage

        Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #5

        Gut Mikh Tappe wrote:

        Please let me know what to do

        Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.

        led mike

        C M 2 Replies Last reply
        0
        • Z zafersavas

          Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #6

          GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          L D 2 Replies Last reply
          0
          • M Mark Salsbery

            GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #7

            Same ocean, different fish, same bait. You have lunch yet? ;P

            led mike

            M 1 Reply Last reply
            0
            • D DSPCottage

              Dear All In visual C++ 2005 I want to change a CString file to char* I used following code But fopen needs a char* for its first parameter Please let me know what to do Regards char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH); CString path = buffer; path = path.Left(path.ReverseFind('\\')+1); path += "result.mp3"; f = fopen( path , "wb");

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #8

              In addition to super_ttd's reply... If you must use fopen(), then you should probably use a CStringA. CString has a generic internal character type, depending on whether UNICODE or _UNICODE is defined. If you want to use the generic text CString, then it will pair nicely with the generic text version of fopen(), which is _tfopen().

              Gut Mikh Tappe wrote:

              char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);

              Bad cast!! GetModuleFileName takes a LPTSTR as its second parameter. Your buffer, therefore, should be a TCHAR type, not char. Mind your types.....if you need a cast to get something to compile, look at WHY. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              D 1 Reply Last reply
              0
              • L led mike

                Same ocean, different fish, same bait. You have lunch yet? ;P

                led mike

                M Offline
                M Offline
                Mark Salsbery
                wrote on last edited by
                #9

                led mike wrote:

                You have lunch yet?

                Heh. I'm going to HAVE to now ;P

                Mark Salsbery Microsoft MVP - Visual C++ :java:

                D 1 Reply Last reply
                0
                • M Mark Salsbery

                  led mike wrote:

                  You have lunch yet?

                  Heh. I'm going to HAVE to now ;P

                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                  D Offline
                  D Offline
                  DSPCottage
                  wrote on last edited by
                  #10

                  Thanks It was great

                  1 Reply Last reply
                  0
                  • Z zafersavas

                    Hi super_ttd, What's the problem with CString::GetBuffer()? Why dont you like it? :)

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #11

                    Read the documentation about CString::GetBuffer and you will see why you have to absolutely avoids it if you don't know exactly what you are doing.

                    Cédric Moonen Software developer
                    Charting control [v1.5] OpenGL game tutorial in C++

                    1 Reply Last reply
                    0
                    • S super_ttd

                      It should do. So, what's wrong ? Do you get an error ? CString has an inner cast Operator (LPCTSTR) which converts the CString object to a const char* (if compiling ANSI) of const wchar_t* (if building unicode), so you should not have to do any much stuff. And please (if you get this advice from anywhere), don't use the CString::GetBuffer()) method at all for such a thing !!! So, to come back to you problem, what is your problem ?


                      D Offline
                      D Offline
                      DSPCottage
                      wrote on last edited by
                      #12

                      Visual C++ 2005 doesn't support (LPCTSTR) cast operator. thus I need an interface to change a CString object to a char* exactly now I am porting an project from VC6 to VC2005 and it is my problem Regards Mahdi

                      1 Reply Last reply
                      0
                      • M Mark Salsbery

                        GetBuffer() is completely unnecessary when you're only reading the string (i.e. when you only need a const pointer to the string's internal buffer). The LPCSTR operator is safe and more efficient. GetBuffer() is for when you need a non-const pointer.

                        Mark Salsbery Microsoft MVP - Visual C++ :java:

                        D Offline
                        D Offline
                        DSPCottage
                        wrote on last edited by
                        #13

                        But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards

                        C M 2 Replies Last reply
                        0
                        • M Mark Salsbery

                          In addition to super_ttd's reply... If you must use fopen(), then you should probably use a CStringA. CString has a generic internal character type, depending on whether UNICODE or _UNICODE is defined. If you want to use the generic text CString, then it will pair nicely with the generic text version of fopen(), which is _tfopen().

                          Gut Mikh Tappe wrote:

                          char buffer[MAX_PATH]; GetModuleFileName(NULL,(LPWCH)buffer,MAX_PATH);

                          Bad cast!! GetModuleFileName takes a LPTSTR as its second parameter. Your buffer, therefore, should be a TCHAR type, not char. Mind your types.....if you need a cast to get something to compile, look at WHY. Mark

                          Mark Salsbery Microsoft MVP - Visual C++ :java:

                          D Offline
                          D Offline
                          DSPCottage
                          wrote on last edited by
                          #14

                          May you please give me an article about UNICODE ? Regards Mahdi

                          R 1 Reply Last reply
                          0
                          • L led mike

                            Gut Mikh Tappe wrote:

                            Please let me know what to do

                            Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.

                            led mike

                            C Offline
                            C Offline
                            Cedric Moonen
                            wrote on last edited by
                            #15

                            led mike wrote:

                            Also reading this might help as well.[^]

                            I think that is the best choice to start with :)

                            Cédric Moonen Software developer
                            Charting control [v1.5] OpenGL game tutorial in C++

                            1 Reply Last reply
                            0
                            • D DSPCottage

                              But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards

                              C Offline
                              C Offline
                              Cedric Moonen
                              wrote on last edited by
                              #16

                              Yes, it is simply because UNICODE is defined by default (which was not the case with VC6). I strongly suggest that you read the article given by Led Mike, things will be much more clear afterwards.

                              Cédric Moonen Software developer
                              Charting control [v1.5] OpenGL game tutorial in C++

                              1 Reply Last reply
                              0
                              • D DSPCottage

                                But it appears that VC2005 doesn't support (LPSTR) or (LPCTSTR) operator. isn't it? Regards

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #17

                                The operator is a member of the CSimpleStringT class, which is the base class of CStringT. The actual operator is PCXSTR (the ATL type), which is the equivalent of LPCTSTR (a Win32 type). Here's your code using generic text types:

                                TCHAR buffer[MAX_PATH];
                                GetModuleFileName(NULL,buffer,MAX_PATH);
                                CString path = buffer;
                                path = path.Left(path.ReverseFind(_T('\\'))+1);
                                path += _T("result.mp3");
                                f = _tfopen( path , _T("wb")); // <-- implicitly uses CString's PCXSTR operator!

                                See Generic-Text Mappings in Tchar.h[^] and for Unicode info: International Programming[^] Mark

                                Mark Salsbery Microsoft MVP - Visual C++ :java:

                                1 Reply Last reply
                                0
                                • L led mike

                                  Gut Mikh Tappe wrote:

                                  Please let me know what to do

                                  Ok I will try. What you need to do is read the documentation.[^] Also reading this might help as well.[^] In my experience not all authors match every reader. So if you find reading those sources don't supply your need 100% keep looking for more. The bottom line is what you need to do is read and study rather than type code and forum messages.

                                  led mike

                                  M Offline
                                  M Offline
                                  Mark Salsbery
                                  wrote on last edited by
                                  #18

                                  led mike wrote:

                                  Also reading this might help as well.[^]

                                  That IS a good link. I bookmarked that to post in the future, Thanks! :beer:

                                  Mark Salsbery Microsoft MVP - Visual C++ :java:

                                  1 Reply Last reply
                                  0
                                  • D DSPCottage

                                    May you please give me an article about UNICODE ? Regards Mahdi

                                    R Offline
                                    R Offline
                                    Rick York
                                    wrote on last edited by
                                    #19

                                    Here are a few : http://www.google.com/search?hl=en&q=UNICODE&btnG=Google+Search[^]

                                    1 Reply Last reply
                                    0
                                    • Z zafersavas

                                      There a various ways: 1)

                                      CString str = "filename";

                                      char charPtr[100];
                                      sprintf(charPtr, "%s", str);

                                      CString str = "filename";
                                      char* charPtr = str.GetBuffer(str.GetLength());

                                      S Offline
                                      S Offline
                                      super_ttd
                                      wrote on last edited by
                                      #20

                                      zafersavas wrote:

                                      str.GetBuffer(str.GetLength());

                                      Memory Leak here ! Why the hell do people use that dangerous GetBuffer() function when it's absolutely useless ? :doh:


                                      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