Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 509
Klaus Nji Avatar asked Feb 25 '23 11:02

Klaus Nji


2 Answers

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);
like image 59
casablanca Avatar answered Mar 27 '23 20:03

casablanca


Console::WriteLine(gcnew System::String(cs3)); 
like image 44
Ben Voigt Avatar answered Mar 27 '23 18:03

Ben Voigt