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

CString to const char* conversion

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 Posts 6 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.
  • S Offline
    S Offline
    sugumar
    wrote on last edited by
    #1

    I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.

    K P M C 4 Replies Last reply
    0
    • S sugumar

      I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.

      K Offline
      K Offline
      Ken Mazaika
      wrote on last edited by
      #2

      I'm not sure if this will work, but have you tried simply setting the LPTSTR equal to the CString? I think CString has an LPTSTR operator if memory serves... -Kmaz

      S 1 Reply Last reply
      0
      • K Ken Mazaika

        I'm not sure if this will work, but have you tried simply setting the LPTSTR equal to the CString? I think CString has an LPTSTR operator if memory serves... -Kmaz

        S Offline
        S Offline
        sugumar
        wrote on last edited by
        #3

        CString is not able to be assigned with LPTSTR, LPTSTR lpszString = filename; gives error error C2440: 'initializing' : cannot convert from 'class CString' to 'unsigned short *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

        1 Reply Last reply
        0
        • S sugumar

          I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          CString has an LPCTSTR operator, so you can pass a CString object to any function that requires a LPCTSTR

          fptr = fopen(m_fileName, _T("r"))

          CString::GetBuffer() returns a LPTSTR, not a LPCTSTR. LPTSTR is a char* LPCTSTR is a const char*


          [

          ](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

          S 1 Reply Last reply
          0
          • P PJ Arends

            CString has an LPCTSTR operator, so you can pass a CString object to any function that requires a LPCTSTR

            fptr = fopen(m_fileName, _T("r"))

            CString::GetBuffer() returns a LPTSTR, not a LPCTSTR. LPTSTR is a char* LPCTSTR is a const char*


            [

            ](http://www.canucks.com)"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

            S Offline
            S Offline
            sugumar
            wrote on last edited by
            #5

            m_fileName is CEdit type. Also i hv to collect string value from CstringArray like this.. CStringArray* aXMLfiles; CString filename; int mlen = aXMLfiles->GetSize(); // write all the XML files for(j=0;jGetAt(j); LPTCSTR lpszString = filename; fptr = fopen( lpszString, "r" ); ////do something//// } still i get error.

            J 1 Reply Last reply
            0
            • S sugumar

              m_fileName is CEdit type. Also i hv to collect string value from CstringArray like this.. CStringArray* aXMLfiles; CString filename; int mlen = aXMLfiles->GetSize(); // write all the XML files for(j=0;jGetAt(j); LPTCSTR lpszString = filename; fptr = fopen( lpszString, "r" ); ////do something//// } still i get error.

              J Offline
              J Offline
              Johan Rosengren
              wrote on last edited by
              #6

              You can take away the extra assignment, this is enough:

              fopen( filename, "r" );

              or this:

              fopen( ( LPCTSTR ) filename, "r" );

              If not, your problem lies elswhere, and you might want to post some more code.

              S 1 Reply Last reply
              0
              • J Johan Rosengren

                You can take away the extra assignment, this is enough:

                fopen( filename, "r" );

                or this:

                fopen( ( LPCTSTR ) filename, "r" );

                If not, your problem lies elswhere, and you might want to post some more code.

                S Offline
                S Offline
                sugumar
                wrote on last edited by
                #7

                still problem continues.I put the code for ur ref. CStringArray* aXMLfiles; int mlen,j; CString filename; FILE *fptr; aXMLfiles = GetFileNames("*.XML"); /// This function retrives me all XML files.. mlen = aXMLfiles->GetSize(); for(j=0;jGetAt(j); fptr = fopen( ( LPCTSTR ) filename, "r" ); if (fptr != NULL){ // do something } } fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

                J 1 Reply Last reply
                0
                • S sugumar

                  still problem continues.I put the code for ur ref. CStringArray* aXMLfiles; int mlen,j; CString filename; FILE *fptr; aXMLfiles = GetFileNames("*.XML"); /// This function retrives me all XML files.. mlen = aXMLfiles->GetSize(); for(j=0;jGetAt(j); fptr = fopen( ( LPCTSTR ) filename, "r" ); if (fptr != NULL){ // do something } } fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.

                  J Offline
                  J Offline
                  Johan Rosengren
                  wrote on last edited by
                  #8

                  As CString has a user-defined operator LPCTSTR, the "or the operator cannot be called"-part of the error message seems to be the interesting part. What is the error number (CXXXX) you get?

                  1 Reply Last reply
                  0
                  • S sugumar

                    I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.

                    M Offline
                    M Offline
                    markkuk
                    wrote on last edited by
                    #9

                    Are you using Unicode? The complaint about "unsigned short*" looks like you are trying to feed wide characters to a function that understands only 8-bit chars.

                    1 Reply Last reply
                    0
                    • S sugumar

                      I need to convert CString value to const char *. I do the same in following way. I also tried with LPCTSTR, still i get same error. CString filename; FILE *fptr; m_fileName.GetWindowText(filename); int nLen = filename.GetLength(); LPTSTR lpszString = filename.GetBuffer (nLen ); fptr = fopen( lpszString, "r" ); But i get error as fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast i need ur help to sole this issue.

                      C Offline
                      C Offline
                      cmk
                      wrote on last edited by
                      #10

                      This is why i stopped using CString. It's an all or nothing proposition. If you compile for UNICODE then it wraps an array of wchar_t (unsigned short). If you compile for ANSI then it wraps an array of char. You can't cast between the two, you must convert. Use wcstombs to go from a wchar_t to char. Use mbstowcs to go from a char to wchar_t. [EDIT] Actually what you should do if using CString is go the whole way and use all the _t funtion versions. e.g. _tfopen() will work with a CString regardless of it being UNICODE or ANSI. Most functions that take a char* have a _t*() version. Check the MSDN doc's. [/EDIT] ...cmk Save the whales - collect the whole set

                      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