?
Solved

CSTRING into normal char[10]

Posted on 1999-06-23
8
Medium Priority
?
336 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
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.

601 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