C ++ / CLI Printing CString content for console

C ++ newbie here with a quick question. How to print CString contents on 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; } 

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

+4
source share
3 answers

I assume that you are compiling with Unicode enabled, but printf is an ANSI function, so it prints only the first character of the line. Use _tprintf to match _T strings:

 _tprintf(_T("%s"), cs3); 
+5
source
 Console::WriteLine(gcnew System::String(cs3)); 
+3
source

You need to give your CString before printing

 printf("%s ", (LPCTSTR)cs3); 

This should work

0
source

All Articles