Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to convert CString to char array
C++
struct ar
{
char value[10];
};

function(CString  data)
{
 ar.value=data;
}
Posted
Updated 25-Aug-11 21:03pm
v2
Comments
CPallini 26-Aug-11 3:11am    
Why do you need it (most of the times you really don't need)?

1. CString::GetBuffer
2. (char*)LPCTSTR(your CString var)
 
Share this answer
 
Some of the other solutions gave me some trouble sometimes truncateing some of the CString, but this one is simple & it works as intended!!

{Please notice the Capital %S in sprintf}

C#
CString z(_T("I love CString!\r\n"));
char sz[100];
sprintf(sz, "%S", z);
 
Share this answer
 
Comments
simsa3d 30-Apr-12 16:51pm    
short and easy! perfect!
Seigfried Hung 18-Oct-15 22:37pm    
Very Useful, much thanks!
Try it :) :
C++
void function(const CString& cszData)
{
  USES_CONVERSION;
  char* pchTmp(T2A(cszData));

  strcpy_s(ar.value, _countof(ar.value), pchTmp);
}
 
Share this answer
 
Comments
Philippe Mori 26-Aug-11 19:49pm    
It does answer the question... but it is not recommanded to do that if not necessary (that is if you cannot change ar structure). See my solution.
If you can change ar, an approch like this one would be a lot better...

C++
class ar
{ 
public:
    void set_value(const CString &data) { m_data = data; }
    CString &get_value() { return m_data; }
    const CString &get_value() const { return m_data; }

private:
    CString m_data;
};

function(const CString &data)
{
    ar.set_value(data);
}
 
Share this answer
 
CString str("you know my name");
char cChar[1024];
sprintf(cChar,"%s",CW2A(str));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900