Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-08-03
6
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

688 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