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

Convert char* to CString

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
6 Posts 4 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.
  • R Offline
    R Offline
    rajanponnalagu
    wrote on last edited by
    #1

    Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.

    S CPalliniC 2 Replies Last reply
    0
    • R rajanponnalagu

      Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.

      S Offline
      S Offline
      Sunil Shindekar
      wrote on last edited by
      #2

      _base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)

      R CPalliniC 2 Replies Last reply
      0
      • S Sunil Shindekar

        _base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)

        R Offline
        R Offline
        rajanponnalagu
        wrote on last edited by
        #3

        struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE; FILE *fleCredentials; fleCredentials = fopen("credentials.crd", "a+"); fprintf(fleCredentials, "%s ",(LPCTSTR)m_Username ); Problem: ======== Only one character of "m_Username" has to be stored in the "credentials.crd". I would like to store the fullname into the file. Help to proceed further.

        1 Reply Last reply
        0
        • S Sunil Shindekar

          _base = m_userName.GetBuffer or _base = new char[] strcpy(_base, m_userName.getbuffer)

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Sunil Shindekar wrote:

          _base = m_userName.GetBuffer

          The above is a mistake, because, unless you want to lock the CString internal buffer forever, then you have to call ReleaseBuffer method, afterwards the _base pointer becomes garbage, please read documentation http://msdn2.microsoft.com/en-us/library/kt26tkzx(VS.71).aspx[^].

          Sunil Shindekar wrote:

          _base = new char[] strcpy(_base, m_userName.getbuffer)

          Better, but still mistakes. You don't need at all GetBuffer, to do this (and you aren't calling ReleaseBuffer afterwards), the LPCTSTR cast operator is enough. Furthermore you don't specify character array size. And, finally, you're supposing that it is an ANSI build. Is It enough? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • R rajanponnalagu

            Hello, I have one structure contains char * variable. I want to store the CString data to char* variable. struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; I have one CString variable like m_Username. I want to store m_Username to char *_base. Could you please help me how to do.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Provided it is an ANSI (not UNICODE build, you can do:

            CString strFoo("foo");
            struct _iobuf iobuf;

            iobuf._ptr = new[strFoo.GetLenght()+1];
            if (iobuf._ptr)
            {
            strcpy(iobuf._ptr, strFoo);
            }
            ...
            ...

            // don't forget to delete the buffer
            if (iobuf._ptr)
            {
            delete [] iobuf._ptr;
            }

            However, I suggest, if you can, to refactor a bit you project (i.e. avoiding such a mixing of string classes and character arrays. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            K 1 Reply Last reply
            0
            • CPalliniC CPallini

              Provided it is an ANSI (not UNICODE build, you can do:

              CString strFoo("foo");
              struct _iobuf iobuf;

              iobuf._ptr = new[strFoo.GetLenght()+1];
              if (iobuf._ptr)
              {
              strcpy(iobuf._ptr, strFoo);
              }
              ...
              ...

              // don't forget to delete the buffer
              if (iobuf._ptr)
              {
              delete [] iobuf._ptr;
              }

              However, I suggest, if you can, to refactor a bit you project (i.e. avoiding such a mixing of string classes and character arrays. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #6

              I think it is an interface so he had to do such crappy things. But interface should have the buffer, because it wants the data, and knows when to release the memory to avoid leaks.:~

              Greetings from Germany

              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