c++-clicstring

C++/CLI Printing contents of CString to console


C++ newbie here with a quick question. How do I print the contents of CString to the Console?

Doing this

int main(array<System::String ^> ^args)
{               
    CString cs1 = _T("Hy");
    CString cs2 = _T(" u");
    CString cs3 = cs1 + cs2;

    Console::WriteLine(cs3);    
    printf("%s", cs3);  
    return 0;
}

outputs "True" and "H" on the console. TIA.


Solution

  • I'm guessing you're compiling with Unicode turned on, but printf is an ANSI function, so it prints only the first character of the string. Use _tprintf to match your _T strings:

    _tprintf(_T("%s"), cs3);