views:

309

answers:

5

I use this snippet to create a console fron inside a dll. That dll gets loaded in a game.

CODE SNIPPET

The console window creates fine. But when i write stuff to it, i just get stuff like "???D??".

I know i have to use the printf() syntax. So i use

wprintf("%s", "test");

Any pointers?

A: 

Your encoding is wrong, as you are using wstring characters, but probably only have an ascii console. Make sure that you are using a unicode font in your console.

Visage
Ahh, i dont need unicode support so ill just rewrite it to ansi functions.
Intosia
He's not using wstring characters as wprintf argument.
xtofl
+2  A: 
Key
Its just a name of the function... Have you looked at the code snippet? If i rename it to 'pants', its still the same.
Intosia
+1  A: 

You're calling wprintf(), the "wide-character" version of the printf() routine. There's one nasty catch with these "wide-character" functions, in that the meaning of "%s" changes:

printf() - "%s" means argument is a normal string, "%S" means it's a wide-character string

wprintf() - "%s" means argument is a wide-character string, "%S" means it's a normal string

So your call to wprintf() is telling it that the argument is a wide-character string, but it's not: change it to

printf("%s", "test");
DavidK
I doubt that the meaning of %s and %S changes depending on function called. The difference between the functions is that wprintf takes the format as an wide character string and printf takes a normal string.
Key
At least on Windows (which is what we're talking about here), it does: see http://msdn.microsoft.com/en-us/library/hf4y5e3w%28VS.100%29.aspx
DavidK
You are correct. I think that is madness.
Key
It does this so its easy to compile both ansi and unicode versions of the same code: Tversionofprint(_T("format %s code"),_T("parameter"));
Anders
A: 

the "%s" format string tells wprintf it should interpret the first argument as a wide character string.

To avoid these mixed-encoding problems, you can use the TCHAR macro's:

LPCTSTR pName = _T("World");
_tprintf( _T("%s, %s"), _T("Hello"), pName );
xtofl
A: 

A good habit is to use portability macro _T() which does nothing when we use ASCII and prepends all strings with L when UNICODE is defined. And using _t prepended functions which are really just macros that map to plain functions when we use ASCII and map to w prepended functions when UNICODE is defined. That way you always have a portable code - which works in both versions:

_tprintf(_T("%s"),_T("test");
justadreamer