C++ MFC

Moderators: Lundin
Number of threads: 3354
Number of posts: 9032

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to convert CString into Char * ? Posted by pasa on 12 Dec 2006 at 12:12 PM
I have variable:
char * str;

and m_str is a member of TextBox Control, its type: CString
And I want when I click on button, str will get the text in the text box.

I used:
str = m_str;


I know it's wrong, but I don't have any better idea. Can anyone help me please?
Report
Re: How to convert CString into Char * ? Posted by velius on 13 Dec 2006 at 10:19 AM
: I have variable:
:
: char * str;
: 

: and m_str is a member of TextBox Control, its type: CString
: And I want when I click on button, str will get the text in the text box.
:
: I used:
:
: str = m_str;
: 

:
: I know it's wrong, but I don't have any better idea. Can anyone help me please?
:

First you need to allocate memory for the char* str to point to.
For example:
char* str = new char[m_str.GetLength() +1];

That would allocate enough memory for a C-Style null terminated string.
After that you'll need to copy the contents of the CString m_str variable to the newly allocated memory pointed to by the char* str variable.

You can look this up on msdn.microsoft.com for more detailed information.

Best of luck.



We'll be an army of theives
Of self-freed slaves
Of mild-mannered maids
We'll fight with whispers and blades
So get ready, a new day is dawning
- The New Wild West -- Jewel

Report
Re: How to convert CString into Char * ? Posted by john_vp2007 on 28 May 2007 at 4:25 AM
: : I have variable:
: :
: 
: : char * str;
: : 
:
: : and m_str is a member of TextBox Control, its type: CString
: : And I want when I click on button, str will get the text in the text box.
: :
: : I used:
: :
: 
: : str = m_str;
: : 
:
: :
: : I know it's wrong, but I don't have any better idea. Can anyone help me please?
: :
:
: First you need to allocate memory for the char* str to point to.
: For example:
:
: 
: char* str = new char[m_str.GetLength() +1];
: 
:
: That would allocate enough memory for a C-Style null terminated
: string.
: After that you'll need to copy the contents of the CString m_str
: variable to the newly allocated memory pointed to by the char* str
: variable.
:
: You can look this up on msdn.microsoft.com for more detailed
: information.
:
: Best of luck.
:
:

: We'll be an army of theives
: Of self-freed slaves
: Of mild-mannered maids
: We'll fight with whispers and blades
: So get ready, a new day is dawning
: - The New Wild West -- Jewel
:
: I can suggest you one moreway.
suppose you have a string csString.
and you want to convert it into a char* then....

char *string = new char[csString.GetLength() +1];
string = csString.GetBuffer(csString.GetLength() +1);
csString.ReleaseBuffer();



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.