Go4Expert

Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   convert char array to CString (http://www.go4expert.com/forums/convert-char-array-cstring-t19300/)

answerme 4Sep2009 11:50

convert char array to CString
 
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

Gene Poole 10Nov2009 21:28

Re: convert char array to CString
 
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 );



All times are GMT +5.5. The time now is 09:32.