Solved

CString to char Array?

Posted on 2001-08-08
10
2,472 Views
Last Modified: 2007-11-27
How do I copy a CString to a Char array?

CString m_elsetSlotString;

char *msg;

m_elsetSlotString = ((LPCSTR)"SLOT 0");

msg[0] = m_elsetSlotString; // Doesn't work
0
Comment
Question by:GoldStrike
10 Comments
 

Expert Comment

by:zhjackal
ID: 6366806
msg = LPCSTR(m_elsetSlotString)
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6366815
Easier (and clearer):

CString strHello = "Hello";
char test[32]; // or use new and sizeof for dynamic memory allocation
strncpy(test, strHello, sizeof(strHello));
0
 
LVL 30

Expert Comment

by:Axter
ID: 6366823
You should use a cast in the strcpy command and the GetLength() function to determine the size.
Example:
CString strHello = "Hello";
char test[32];
strncpy(test, (LPCTSTR)strHello, strHello.GetLength());

or
strcpy(test, strHello.operator LPCTSTR(), strHello.GetLength());
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 30

Expert Comment

by:Axter
ID: 6366830

CString m_elsetSlotString;
//The following method is used to get a constant pointer to a CString object
const char *msg = m_elsetSlotString.operator LPCTSTR();

//The following method is used to get a pointer to a CString object
//You should use this method if you want to modify the data that msg is pointing to
char *msg = m_elsetSlotString.LockBuffer();
//Do something with msg variable here.

//Now that you're done , you need to UnLock the buffer
m_elsetSlotString.UnlockBuffer();

You can also use GetBuffer() and ReleaseBuffer() functions for similar above requirements.
0
 
LVL 4

Expert Comment

by:IainHere
ID: 6367031
Just to clarify Axter's comment, if the string you want can be constant, then define it as const char* and the conversion is implicit (that is, the operator LPCTSTR() is called without you needing to specify if)

msg = m_elseSlotString;
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6367453
hi zhjackal,
Welcome to Experts Exchange.  Please check the guidelines on answering questions.  See: http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#6

The experts in this topic area have agreed to make their posts as Comments, rather than Answers.   So please post Comments, like everybody else.  Thanks!

Anyway, your "answer" simply sets up a pointer to the char array (in an unsafe sort of way), it does not copy its data.

-- Dan
0
 

Author Comment

by:GoldStrike
ID: 6368544
Axter thanks that worked,

I will award you the points but I had a follow on question which is why I increased the value.

How do I copy the upper portion of the strhello string using strncpy.

In other words, let's say strhello contains "hello", but I only wanted to copy "llo" to the test array, How would I accomplish this.

thanks for all the inputs,
I'm sorry if I screwed this up, maybe this should have been a seperate question.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6368906
Why copy?
If you use

   m_elsetSlotString.GetBuffer(0);

it will drop a "char *" in your lap.
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 6369526
>>How do I copy the upper portion of the strhello string
>>using strncpy.
The pointer to strhello can be treated like a normal "const char*" pointer.

Example:
CString strHello = "Hello";
char test[32];
strcpy(test, (LPCTSTR)strHello);//Normal copy
AfxMessageBox(test);//Show contents of test

//copy starting at the third letter in the string
strcpy(test,((LPCTSTR)strHello) + 2 );//Test will equal "llo"
AfxMessageBox(test);//Show contents of test
0
 

Author Comment

by:GoldStrike
ID: 6369666
Thanks Axter
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 196
best sources to up-to-date in C++? 8 79
How to convert MFC APP to Win32 APP. 19 79
C++ error cannot convert from std::string to const char * 6 179
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question