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 LPCSTR.

CString to LPCSTR.

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 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.
  • U Offline
    U Offline
    uday kiran janaswamy
    wrote on last edited by
    #1

    hi all, i want to convert CString to LPCSTR. /===================================== CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text". //==================================== Please let me know if any solution for this. its urgent for me

    Uday kiran

    M A A T 4 Replies Last reply
    0
    • U uday kiran janaswamy

      hi all, i want to convert CString to LPCSTR. /===================================== CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text". //==================================== Please let me know if any solution for this. its urgent for me

      Uday kiran

      M Offline
      M Offline
      Mass Nerder
      wrote on last edited by
      #2

      uday kiran janaswamy wrote:

      CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text".

      You can't use LPCSTR here because of the constantness. But how about this: LPTSTR result[150]; _tcsncpy(result, str.GetString(), 150); hth, Christof

      U D 2 Replies Last reply
      0
      • M Mass Nerder

        uday kiran janaswamy wrote:

        CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text".

        You can't use LPCSTR here because of the constantness. But how about this: LPTSTR result[150]; _tcsncpy(result, str.GetString(), 150); hth, Christof

        U Offline
        U Offline
        uday kiran janaswamy
        wrote on last edited by
        #3

        hi christopher, i am getting this type of error as i copy your code. please help me out. '_tcsncpy' : cannot convert parameter 1 from 'LPSTR [150]' to 'char *

        Uday kiran

        J 1 Reply Last reply
        0
        • U uday kiran janaswamy

          hi all, i want to convert CString to LPCSTR. /===================================== CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text". //==================================== Please let me know if any solution for this. its urgent for me

          Uday kiran

          A Offline
          A Offline
          Anilkumar K V
          wrote on last edited by
          #4

          From MSDN // example for CString::GetBuffer CString s( "abcd" ); #ifdef _DEBUG afxDump << "CString s " << s << "\n"; #endif LPTSTR p = s.GetBuffer( 10 ); strcpy( p, "Hello" ); // directly access CString buffer s.ReleaseBuffer( ); #ifdef _DEBUG afxDump << "CString s " << s << "\n"; #endif

          1 Reply Last reply
          0
          • U uday kiran janaswamy

            hi all, i want to convert CString to LPCSTR. /===================================== CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text". //==================================== Please let me know if any solution for this. its urgent for me

            Uday kiran

            A Offline
            A Offline
            Abris
            wrote on last edited by
            #5

            I have found this page usefull when I convert between different types: http://www.codeproject.com/cpp/data_conversions.asp[^] There you will find many other conversions as well.

            1 Reply Last reply
            0
            • U uday kiran janaswamy

              hi all, i want to convert CString to LPCSTR. /===================================== CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text". //==================================== Please let me know if any solution for this. its urgent for me

              Uday kiran

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              why don't you just do :

              CString str = "hello world";

              (LPCSTR)str; // used as you like
              // either used with strcpy to be copied, or passed as a function parameter


              Don't know where to start ?
              Refer the Forums Guidelines and ask a friend

              [VisualCalc 3.0][Flags Beginner's Guide]

              U 1 Reply Last reply
              0
              • U uday kiran janaswamy

                hi christopher, i am getting this type of error as i copy your code. please help me out. '_tcsncpy' : cannot convert parameter 1 from 'LPSTR [150]' to 'char *

                Uday kiran

                J Offline
                J Offline
                James R Twine
                wrote on last edited by
                #7

                The person that replied to you made a typo, the code should be:

                TCHAR result[150];
                _tcsncpy(result, str.GetString(), 150);

                If you are using a CString, you there is a built in operator that will return an LPCTSTR, so you do not have to use GetBuffer(...) or anything like that to get a const pointer.    Peace!

                -=- James
                Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
                Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
                See DeleteFXPFiles

                1 Reply Last reply
                0
                • T toxcct

                  why don't you just do :

                  CString str = "hello world";

                  (LPCSTR)str; // used as you like
                  // either used with strcpy to be copied, or passed as a function parameter


                  Don't know where to start ?
                  Refer the Forums Guidelines and ask a friend

                  [VisualCalc 3.0][Flags Beginner's Guide]

                  U Offline
                  U Offline
                  uday kiran janaswamy
                  wrote on last edited by
                  #8

                  Thank you very much.

                  Uday kiran

                  1 Reply Last reply
                  0
                  • M Mass Nerder

                    uday kiran janaswamy wrote:

                    CString str = "Hellow this is a Text"; LPCSTR result[150]; -->how we get the str value in result ?. i.e result = "Hellow this is a Text".

                    You can't use LPCSTR here because of the constantness. But how about this: LPTSTR result[150]; _tcsncpy(result, str.GetString(), 150); hth, Christof

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

                    Mass Nerder wrote:

                    _tcsncpy(result, str.GetString(), 150);

                    When using a method that is specific to a particular VC++ version, it helps to state such.


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

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

                    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