printf / stdout do not work on win32

Hi guys,

I have been trying to get printf/fputs(“”, stdout) to work yesterday. Unfortunately, to no avail.

Out of the box, nothing gets printed to the console on Windows. I am using a couple of external libraries that make heavy use of the console (for debugging purposes) such as lualib.

What is the preferred way to get the output to stdout working?
Well, I could of course change all the fputs’ in lualib to CCLOG’s. That puts me in the position where I have to redo all the replacing after I update a library. So, I’d rather not go there.

Any hints on that will be greatly appreciated.

Thanks, Walter

Oh, I forgot …

what I tried to redirect stdout and stderr I used a snippet like this:

FILE *stream;
stdout_buf = (new char[STDOUT_BUFFER_LEN]);
freopen_s(&stream, “NUL”, “a”, stdout);
setvbuf(stdout, stdout_buf, _IONBF, STDOUT_BUFFER_LEN);

Well, as I said. That does not work. :frowning:

Cheers

It seems you’re not very familiar to VisualStudio :slight_smile:
You can try to create a new win32 project without cocos2d, File~~>New~~>Project~~>Win32~~>Win32 Console Application. No matter you choose “Windows application” or “Console application”, printf/fputs doesn’t output the string into VisualStudio output window.

I write a code snip to show this

You can see that, printf & fputs output the string into a new console window, while OutputDebugStringA writes it into VS output window. CCLOG is using OutputDebugStringA to do this, too.

In your situation, my suggestion is simply hack the source, just write codes like

#ifdef WIN32
    #define printf OutputDebugStringA
    #define fputs(string, des) OutputDebugStringA(string)
#endif

then include this header file in your cpp files. Doing this to replace the entry of printf/fputs. But take care there must be only fputs(“string”, stdio) in your code, avoid to hack fputs(“string”, pFile) if the code really want to write file.

Hope this answer may help you :slight_smile:

Hi,

thanks for the answer. Yep … I’m not much of Win32 coder. I’m not even a big fan of C++. ;) It’s been a long time since my last Windows project.
Anyway, I guess I’m out of luck then. External libraries like libCurl oder libLua will not be able to print any debug output unless I’m willing to change every fput call, which makes patching the libs hard later.
Just redefining it is not an option either because writing to files is necessary.
I have used Airplay SDK before and there stdout printed nicely to the VStudio console.
Uh, thinking about it … Maybe I will do the redefine of fputs … but I redefine it to another function! This function will then check the second parameter and if it’s stdout or stderr it forwards the call to OutputDebugStringA. If not it calls the original fputs.
That may even work!
Cheers :
)