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 problem in visual C++ 2005

CString problem in visual C++ 2005

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
8 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.
  • D Offline
    D Offline
    DSPCottage
    wrote on last edited by
    #1

    following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh

    M S 2 Replies Last reply
    0
    • D DSPCottage

      following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh

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

      What's the actual error? Second, do you really need the explicit cast?  What is CSTRING_VARIABLE? You should be able to use a CString without the cast there, unless there's a conflict with char/wchar_t types. Mark

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

      D 1 Reply Last reply
      0
      • M Mark Salsbery

        What's the actual error? Second, do you really need the explicit cast?  What is CSTRING_VARIABLE? You should be able to use a CString without the cast there, unless there's a conflict with char/wchar_t types. Mark

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

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

        fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.

        M Richard Andrew x64R 2 Replies Last reply
        0
        • D DSPCottage

          fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.

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

          You shouldn't need the cast.  CString has a cast operator that does this for you. I'm still wondering what the actual compiler error is.

          Gut Mikh Tappe wrote:

          we can cast a CString variable to a char* by using (LPCTSTR)

          That's not a valid cast.  LPCTSTR is a constant pointer to a TCHAR. TCHAR can be a char or a wchar_t, depending on if _UNICODE is defined. Without knowing the error message, I'm guessing your build settings are for unicode, in which case you should either change the project settings to not use unicode, use the correct CStringT type, or use _tfopen() instead of fopen(). Regardless, you should NOT need the cast.  Casts should be used only when absolutely necessary, because they can hide errors, exactly as demonstrated here. Mark

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

          S 1 Reply Last reply
          0
          • D DSPCottage

            fopen function need one const char * as its first parameter. in visual C++ 6 we can cast a CString variable to a char* by using (LPCTSTR) . but this simple methodology does not work for visual C++ 2005. Because we want to upgrade one project from VC6 to VC2005, thus it is neccessary to change it. Now I want a simple code snippet which converts a CString variable to char* . Regards Gut Mikh.

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Try: LPCSTR Instead of LPCTSTR

            1 Reply Last reply
            0
            • M Mark Salsbery

              You shouldn't need the cast.  CString has a cast operator that does this for you. I'm still wondering what the actual compiler error is.

              Gut Mikh Tappe wrote:

              we can cast a CString variable to a char* by using (LPCTSTR)

              That's not a valid cast.  LPCTSTR is a constant pointer to a TCHAR. TCHAR can be a char or a wchar_t, depending on if _UNICODE is defined. Without knowing the error message, I'm guessing your build settings are for unicode, in which case you should either change the project settings to not use unicode, use the correct CStringT type, or use _tfopen() instead of fopen(). Regardless, you should NOT need the cast.  Casts should be used only when absolutely necessary, because they can hide errors, exactly as demonstrated here. Mark

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

              S Offline
              S Offline
              Sarath C
              wrote on last edited by
              #6

              I do agree with Mark's comment. When you create a new project in Visual C++ 2005, probably you might have not noticed the "Use Unicode Libraries" in the Wizard. While you are dealing with these it's better to use tchar functions. You can get those functions just by including #include Please post the error code if possible.

              -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

              My blog - Sharing My Thoughts, An Article - Understanding Statepattern

              L 1 Reply Last reply
              0
              • D DSPCottage

                following code snippet works in VC++ 6 file = fopen((LPCTSTR)CSTRING_VARIABLE,"wb+") but in VC 2005 above code can not compile because of CString and casting problem. how I can convert above code to be compatible with visual C++ 2005. Regards Gut Mikh

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                Drop the casts. Use code like this:

                #include <tchar.h>
                 
                // .... Stuff here ...
                 
                file = _tfopen(CSTRING_VARIABLE, _T("wb+"));

                Steve

                1 Reply Last reply
                0
                • S Sarath C

                  I do agree with Mark's comment. When you create a new project in Visual C++ 2005, probably you might have not noticed the "Use Unicode Libraries" in the Wizard. While you are dealing with these it's better to use tchar functions. You can get those functions just by including #include Please post the error code if possible.

                  -Sarath_._ "Great hopes make everything great possible" - Benjamin Franklin

                  My blog - Sharing My Thoughts, An Article - Understanding Statepattern

                  L Offline
                  L Offline
                  lostangels
                  wrote on last edited by
                  #8

                  i do agree with Sarath. when you change the charset in the project property sheet from "Use Unicode Libraries" to "Use MBCS Libraries",the compile problem with CString will disappear at once.

                  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