Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC8.0
How to convert CString to char array
struct ar
{
char value[10];
};
 
function(CString  data)
{
 ar.value=data;
}
Posted 25-Aug-11 21:57pm
Edited 25-Aug-11 22:03pm
Prerak Patel140.6K
v2
Comments
CPallini at 26-Aug-11 3:11am
   
Why do you need it (most of the times you really don't need)?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

1. CString::GetBuffer
2. (char*)LPCTSTR(your CString var)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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}
 
CString z(_T("I love CString!\r\n"));
char sz[100];
sprintf(sz, "%S", z);
  Permalink  
Comments
simsa3d at 30-Apr-12 16:51pm
   
short and easy! perfect!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try it Smile | :) :
void function(const CString& cszData)
{
  USES_CONVERSION;
  char* pchTmp(T2A(cszData));
 
  strcpy_s(ar.value, _countof(ar.value), pchTmp);
}
  Permalink  
Comments
Philippe Mori at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you can change ar, an approch like this one would be a lot better...
 
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);
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

CString str("you know my name");
char cChar[1024];
sprintf(cChar,"%s",CW2A(str));
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 George Jonsson 145
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,382


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 21 Nov 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100