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. Convert CString to const char * in embedded VC++

Convert CString to const char * in embedded VC++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++hardware
19 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.
  • N Naveen

    why u want to use char*. If u use TCHAR*..the problem will be solved..

    houari_id wrote:

    CString __filepath; __filepath = dlg.GetPathName(); int x = __filepath.GetLength(); char *filename; filename = new char [x]; for (int i=0;i This is not a good way.. Use _tcscpy instead nave

    H Offline
    H Offline
    houari_id
    wrote on last edited by
    #8

    I've tried this way: char *filename; filename = new char [x]; _tcscpy(filename, __filepath); And when I compile the code, this error occurs: error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'unsigned short *' Houari

    N D 2 Replies Last reply
    0
    • H houari_id

      I've tried this way: char *filename; filename = new char [x]; _tcscpy(filename, __filepath); And when I compile the code, this error occurs: error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'unsigned short *' Houari

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #9

      u try this way TCHAR *filename; filename = new TCHAR[x]; _tcscpy(filename, __filepath); nave

      1 Reply Last reply
      0
      • H houari_id

        I've tried this way: char *filename; filename = new char [x]; _tcscpy(filename, __filepath); And when I compile the code, this error occurs: error C2664: 'wcscpy' : cannot convert parameter 1 from 'char *' to 'unsigned short *' Houari

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #10

        houari_id wrote:

        char *filename; filename = new char [x];

        With Unicode, use wchar_t instead.


        "The largest fire starts but with the smallest spark." - David Crow

        "Judge not by the eye but by the heart." - Native American Proverb

        H 1 Reply Last reply
        0
        • D David Crow

          houari_id wrote:

          file = fopen(__filepath, "rb");

          What is the value of __filepath at this point? When fopen() fails, what is the value of errno?

          houari_id wrote:

          file = fopen(__filepath, "rb");

          . If you are using Unicode, use _wfopen() instead.


          "The largest fire starts but with the smallest spark." - David Crow

          "Judge not by the eye but by the heart." - Native American Proverb

          H Offline
          H Offline
          houari_id
          wrote on last edited by
          #11

          This error occurs during the compiling the code: error C2664: 'fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' Actually, it is fine in VC++ isn't it? In MFC, I just put file = fopen (dlg.GetPathName(), "rb"); and it's work fine. But in eVC++, this seems not OK. Houari

          M 1 Reply Last reply
          0
          • H houari_id

            Thank you for your suggest, Viorel. But when I compile the code, this error still occurs: error C2664: 'fopen' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *' I also tried using TCHAR as Milton suggest, the error becomes error C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short *' to 'const char *' Houari

            V Offline
            V Offline
            Viorel
            wrote on last edited by
            #12

            From the first error messages it seems that your application is actually non-Unicode. See the project properties in order to check this (General --> Character Set). The solution with TCHAR should work in both cases, so the second error message is strange. -- modified at 9:24 Tuesday 20th June, 2006 Actually in order to work in both cases, you should use _tfopen instead of fopen and _wfopen. Instead of char or wchar_t, use TCHAR. Define literal strings with _T("...") macro.

            1 Reply Last reply
            0
            • D David Crow

              houari_id wrote:

              char *filename; filename = new char [x];

              With Unicode, use wchar_t instead.


              "The largest fire starts but with the smallest spark." - David Crow

              "Judge not by the eye but by the heart." - Native American Proverb

              H Offline
              H Offline
              houari_id
              wrote on last edited by
              #13

              No, both data type TCHAR and wchar_t are not working, this error still there for both: cannot convert parameter 1 from 'unsigned short *' to 'const char *' Or is it something wrong with my embedded VC++ ? :(( Houari

              D 1 Reply Last reply
              0
              • H houari_id

                This error occurs during the compiling the code: error C2664: 'fopen' : cannot convert parameter 1 from 'class CString' to 'const char *' Actually, it is fine in VC++ isn't it? In MFC, I just put file = fopen (dlg.GetPathName(), "rb"); and it's work fine. But in eVC++, this seems not OK. Houari

                M Offline
                M Offline
                Milton Karimbekallil
                wrote on last edited by
                #14

                try, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB

                V D 2 Replies Last reply
                0
                • M Milton Karimbekallil

                  try, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB

                  V Offline
                  V Offline
                  Viorel
                  wrote on last edited by
                  #15

                  Actually in order to work in both cases, you should use _tfopen instead of fopen and _wfopen:

                  file = _tfopen(dlg.GetPathName(), _T("rb"));
                  
                  H 1 Reply Last reply
                  0
                  • V Viorel

                    Actually in order to work in both cases, you should use _tfopen instead of fopen and _wfopen:

                    file = _tfopen(dlg.GetPathName(), _T("rb"));
                    
                    H Offline
                    H Offline
                    houari_id
                    wrote on last edited by
                    #16

                    Waaa!! It works!! :) Thank you very much Viorel... and the others too... thank you... Houari

                    1 Reply Last reply
                    0
                    • H houari_id

                      No, both data type TCHAR and wchar_t are not working, this error still there for both: cannot convert parameter 1 from 'unsigned short *' to 'const char *' Or is it something wrong with my embedded VC++ ? :(( Houari

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #17

                      Are you still trying to use fopen()? If so, you are erroneously mixing MBCS and Unicode.

                      // MBCS
                      char szFile[MAX_PATH];
                      fopen(szFile, "r");

                      // Unicode
                      wchar_t szFile[MAX_PATH];
                      _wfopen(szFile, "r");

                      // Portable
                      TCHAR szFile[MAX_PATH];
                      _tfopen(szFile, "r");


                      "The largest fire starts but with the smallest spark." - David Crow

                      "Judge not by the eye but by the heart." - Native American Proverb

                      1 Reply Last reply
                      0
                      • M Milton Karimbekallil

                        try, file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb"); cheers.. mIlton KB

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #18

                        Milton KB wrote:

                        file = fopen ((TCHAR*)dlg.GetPathName().GetBuffer(), "rb");

                        There is no need to call GetBuffer() as the object is not being modified.


                        "The largest fire starts but with the smallest spark." - David Crow

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • H houari_id

                          Hi all.. I've been stuck for a long time in this problem. It seems very easy to convert CString to a const char * or char * in VC++, but it really does not work for embedded VC++. My code is simply trying to open a file, using fopen with file path taken from CFileDialog GetPathName(). CFileDialog dlg (TRUE, _T("maf"), NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_EXPLORER, szFilters); if (IDOK == dlg.DoModal()) { CString __filepath = dlg.GetPathName(); file = fopen(__filepath, "rb"); } I've tried to read each single character from CString (by iteratively using GetAt() function) as follows CString __filepath; __filepath = dlg.GetPathName(); int x = __filepath.GetLength(); char *filename; filename = new char [x]; for (int i=0;i<x;i++) { filename[i] = (char)__filepath.GetAt(i); } But it does not work for UNICODE text. Help me.. :(( Houari

                          M Offline
                          M Offline
                          Michael Dunn
                          wrote on last edited by
                          #19

                          You need to read up on character sets, and the differences between ANSI and Unicode builds. Start here: clickety[^]

                          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer

                          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