I am writing an experimental networking program, basically a test program for learning networking. I am using SDL and SDL_net in Code::Blocks with mingw, so the console output was being directed to stdout.txt. I searched around and found that you can fix this by including after SDL_Init():
freopen("CON", "w", stdout); //stops redirect of output
freopen("CON", "w", stderr); //and errors...
This worked perfectly, but only when building and running the program in the IDE: when run outside of the IDE ( e.g. double clicking on the program) the program runs properly, with the exception of the console output, which is still blank. As the program is supposed to be a console program this is a severe problem... I don't want to have to always run the program in the IDE to use it.
Any solution is apreciated but I would prefer that it was an alteration to the code, although in a pinch a batch file will do (I've read a couple of posts where this is the only thing that works, but they didn't go into much detail, so I can't replicate it). Thanks.
SDL 2.0 disables the console by default, or rather, it does not enable it.
Since compiling with -mwindows disables the console, stdout does not point to anything. SDL_Log is a little fancier and can find the console using the windows API, but it can't capture stdin, because cmd.exe stole it :(.
SDL does not claim the console for various reasons, most likely because the program was compiled with -mwindows. Specifying WinMain as the entry point might also make cmd.exe reclaim the console. From what I've read SDL_main might redirect stdout and stderr.
You could probably get away with #undef'ing main, and/or
AllocConsole(); after SDL_init(...);freopen("CON", stdout)-mwindows (#undef'ing main doesn't seem to have any effect)but you should really just redirect stdout to e.g. cat, with main | cat (Always follow the rule "Don't #undef main unless you have to").
To capture stdout for MinGW, simply redirect your program to cat: main | cat. This is a hack which simply makes sure stdout and stderr don't point to nothing.
To capture both stdin and stdout, compile without -mwindows to create a Windows console application. This will open a new window if necessary.
Note: It is especially important to flush output when using one of the above methods. Relying on line buffering is bad practice anyway.
(I couldn't post this yesterday because I didn't have enough reputation)
Ok, did a little experimenting on the lines of the batch file. My resulting and (almost) working batch file:
program.exe
Didn't realise it would be this simple, but still can't understand why double clicking on the program doesn't work. I said almost working because after the client connects to the server the console blanks out, so there is still an issue. So, I still would really apreciate any help with this problem.
(End of yesterday's prospective post)
(Begining of today's answer)
I tried Emartel's suggestions but it still didn't work. Did some testing and discovered that an infinite loop of printing a empty string was causing the issue of the blank screen after connecting the client. Fixed the loop and now it works properly, although I still have to use the batch script.
Would apreciate knowing if someone ever figures out why double-clicking doesn't work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With