convert char array to CString

answerme's Avatar, Join Date: Dec 2007
Ambitious contributor
How should i convert char array to CString
Code:
In my user.cpp file 
char  Spread_name[20, Private_group[20];
CString a;
a.Format(_T("User: connected to localhost;%s with private group %s\n"), Spread_name, Private_group );

In Spread_name, Private_group value are coming all right (showing proper string)
but after giving a breakpoint my a.format value out put is some what like this

Code:
User: connected to localhost;[][][][][[][][][][] with private group[][][]][[]][][]][]
Does anyone knows the solution
0
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
You're probably using a UNICODE build so CString is a CStringW and its Format member expects wide string arguments (wchar_t and not char). You an convert it in the format string though by using a capital 'S' as so:
Code:
char  Spread_name[20], Private_group[20];
CString a;
a.Format(_T("User: connected to localhost;%S with private group %S\n"), Spread_name, Private_group );
or just use wchar_t instead of char:

Code:
wchar_t  Spread_name[20, Private_group[20];
CString a;
a.Format(_T("User: connected to localhost;%s with private group %s\n"), Spread_name, Private_group );
or just use the TCHAR type to let the compiler decide based on the _UNICODE setting:
Code:
TCHAR  Spread_name[20, Private_group[20];
CString a;
a.Format(_T("User: connected to localhost;%s with private group %s\n"), Spread_name, Private_group );