?
Solved

CSTRING into normal char[10]

Posted on 1999-06-23
8
Medium Priority
?
335 Views
Last Modified: 2010-04-02
Hello,
I think it is simple
How can I convert a CSTRING to char
0
Comment
Question by:MIKE2S
[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
8 Comments
 
LVL 9

Accepted Solution

by:
jasonclarke earned 40 total points
ID: 1198284
try something like:

#include <afx.h>
#include <string.h>
#include <iostream.h>

void main()
{
      char str[10];
      CString cstr("Hello");
      strcpy(str,cstr);

      cout << str << endl;
}

0
 
LVL 3

Expert Comment

by:Iexpert
ID: 1198285
Use the following member function:

LPTSTR GetBuffer( int nMinBufLength );

i.e

CString MFC_string = "Test";

char* ptrCstring = MFC_string.GetBuffer(10); //Need 10 bytes to work with
*ptrCstring = 'B';
MFC_string.ReleaseBuffer(-1); //-1 => ReleaseBuffer calls strlen to set the len

------------------------------------------------------
If you want a copy of the string use the strcpy function on the
returned pointer from GetBuffer, e.g.

CString MFC_string = "Test";

char ptrCstring[10];

strcpy(MFC_string.GetBuffer(0),ptrCstring);
MFC_string.ReleaseBuffer();


0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1198286
You don't need to use GetBuffer because CString has an operator LPCTSTR() that is called implicitly when you make the call to strcpy which is fine if all you want to do is copy the contents of the CString (modifying it is a different matter), although it does make sense to use the GetLength() method to make sure that it will fit.

Note strcpy looks like:

strcpy(char* dest, char* src)

so:

char ptrCstring[10];
strcpy(MFC_string.GetBuffer(0),ptrCstring);

will copy an uninitialised char array onto the CStrings buffer which is unlikely to be what you want.


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.

 

Expert Comment

by:JHuang
ID: 1198287
CString string;
char ch[10];

////First, check the length of CString, if it is short than 10.

strcpy( ch,  (LPCTSTR)string );


Easy! I tested it.

strcpy(ch, string), won't work.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1198288
JHuang perhaps you should try to compile the example I gave in the answer.  You will find that both it and
strcpy(ch,string) work fine.  

The compiler must make the conversion implicitly, the C style cast is not required.  (Better to avoid C style casts all together if you can).
0
 

Expert Comment

by:mmachie
ID: 1198289
>Note strcpy looks like:
>
>strcpy(char* dest, char* src)

if it looked like the above the implicit conversion wouldn't happen.

actually strcpy looks like:
strcpy( char *s1, const char *s2 )


0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1198290
mmachie, you are correct, but the important thing was that the destination is the first parameter.
0
 

Author Comment

by:MIKE2S
ID: 1198291
I accept jason clarks answer. Works fine thanks to all!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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