1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

convert char array to CString

Discussion in 'MFC' started by answerme, Sep 4, 2009.

  1. answerme

    answerme New Member

    Joined:
    Dec 17, 2007
    Messages:
    114
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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 );
    
     

Share This Page