Why are stdout.txt and stderr.txt created all over the place and how can I change that?

When you create a file-association to a SDL program and start it by doubleclicking an associated file, you will notice that stdout.txt and stderr.txt are writting in the directory where the associated file was. If you do not write anything to stdout or stderr these files will also automaticly be deleted.

With the precompiled SDLmain.dll binary downloaded from the SDL site you cannot prevent the stdout/stderr from being redirected to textfiles. What you can do is compile SDLmain yourself with the NO_STDIO_REDIRECT compiler flag or alternatively not use SDLmain at all.

Note that not using SDLmain will break portability and is not recommended.

That said sometimes it's nicer to just have the stdout.txt and stderr.txt written to the directory where the executable is.

You can do this with a little trickery:

    #include "SDL/SDL.h"

    #ifdef WIN32
        #include "sdl_winmain.h"
        #include "SDL/SDL_main.h"

Where sdl_winmain.h is in your own project directory and is a rewritten copy of src/main/SDL_win32_main.c from the SDL source package. This way you will still have portability for other platforms but not get stdout/stderr files all over the place in windows.

What you'll want to do is replace the stdoutPath and stderrPath with the path the executable is in:

    string programPath(argv[0]);
    int last = programPath.find_last_of("\\");
    if (last > 1) {
        strncpy(stdoutPath, argv[0], last);
    } else {
        _getcwd( stdoutPath, sizeof( stdoutPath ) );    // if separator is not found, use the current working directory anyway
    strncpy(stderrPath, stdoutPath, sizeof( stderrPath ) );

Like so...

Note that when compiling this on windows you should nolonger link with -lSDLmain because that part of SDL has now become part of your own program.

FAQ_Windows_Where_stdout_/_stderr (last edited 2008-04-17 08:18:30 by localhost)