Solved

Conversion from CString to char *s [ C style string]

Posted on 2004-08-03
6
399 Views
Last Modified: 2008-01-09
Hi folks:

How do I convert  a CString to char *  [C- style string]? Thanks in advace
0
Comment
Question by:rxraza
  • 3
  • 2
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11706406
To simply point to the contents of the string:

CString str;
str = "test";

const char *strPtr;
strPtr = str;

If you want to copy contents to a char array:

char buffer[200];  // arbitrary size, you choose
strcpy(buffer, str);
0
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
ID: 11706491
CString str = "What ever you want";

char pString[200];

strcpy (pString, str.operator LPCTSTR ()); // or use operator LPCTSTR () whenver you need a const C style string.

To get a pointer to a buffer you are aloowed to modify use
str.GetBuffer () ; // or GetBufferSetLength ()
and when finished using this buffer call
str.ReleaseBuffer (-1); // -1 if the string is 0 terminated else you need to supply the number of valid chars.
0
 

Author Comment

by:rxraza
ID: 11706632
I like operator LPCTSTR() approach better.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:SteH
ID: 11706663
In the comment from  jaime_olivares the operator LPCTSTR () will be called as well. In his pointer assignment
strPtr= str;
it will be used implicitely.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11706759
>In the comment from  jaime_olivares the operator LPCTSTR () will be called as well. In his pointer assignment
It is not necessary because in my example strPtr is of type: const char *
Try to compile and gives you no warning

In steh comment:
strcpy (pString, str.operator LPCTSTR ()); // or use operator LPCTSTR () whenver you need a const C style string.

str.operator LPCTSTR(),   or better (LPCTSTR)str     is not necessary because second argument of strcpy expect a const char *, and LPCTSTR returns a const char *
0
 
LVL 13

Expert Comment

by:SteH
ID: 11706862
The intention of my comment was to make rxraza aware that
CString str ("xxxx");
const char* pStr = str; // this line will call operator LPCTSTR () implicitly.

You don't need to write it explicitly. But since it is called why don't write it to show that a conversion/casting is happening here.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now