c++consolesdlcodeblocksfreopen

SDL Console output works when debuging, but not when run with the exe


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.


Solution

  • Did you take a look at the SDL Console FAQ?

    They provide many suggestions, including:

    First try

    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    

    If it doesn't work (like in your case), try

    #include <fstream>
    #include <iostream>
    using namespace std;
    ....
    ofstream ctt("CON");
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    ...
    ctt.close();
    

    OR

    FILE * ctt = fopen("CON", "w" );
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
    ...
    ctt.close();
    

    Another option is to recompile the SDLmain library or add the SDLmain code to your project and stop linking against the library.