# cout to console in SDL

## Recommended Posts

cignox1    735
I find it very hard using the debugger provided with Dev-C++ as I cannot make him stop where I want and check the value of every variable, so I would like to print them on the console of course, not on the actual SDL window, but on the dos prompt). It happens that cout doesn't work: is there a way to get cout working as always? Thank you all!

##### Share on other sites
happybara    312
try to put these lines in your code

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

##### Share on other sites
Drew_Benton    1861
If that does not work, make sure you are outputting endl's at the end of the line. I opened up a test project and w/o them, I got no text, but after I added them, it worked fine.

- Drew

Oluseyi    2103

##### Share on other sites
cignox1    735
happybara and Drew_Benton: don't know why, but I tried both the tips and it still doesn't work.. my fault?

Oluseyi: I'm not using the win32 api. The project was initially set up with the SDL template of Dev-C++ wich includes windows.h just in order to use MessageBox(). I would like to keep the code as portable as possible, using only c++ standard libraries... thank you anyway for the link, it may come handy someday.

##### Share on other sites
Drew_Benton    1861
Ok then give C-styled output a try.

#include <stdio.h>printf("This is a test!\n");int val = 5;printf("Value of val: %i\n",val);

And as a side note, you are outputting *before* the breakpoint correct?

- Drew

##### Share on other sites
sand_man    100
The above wont work because the stdout is no longer the console it is stdout.txt
I did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

##### Share on other sites
cignox1    735

Quote:
 And as a side note, you are outputting *before* the breakpoint correct?

I'm not debugging now, I'm just trying to make it work -sigh-

##### Share on other sites
Easca    163
Quote:
 Original post by sand_manThe above wont work because the stdout is no longer the console it is stdout.txtI did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

One solution is to recompile SDL.

##### Share on other sites
Rhaal    754
Quote:
Original post by Easca
Quote:
 Original post by sand_manThe above wont work because the stdout is no longer the console it is stdout.txtI did see a solution to this problem not too long ago here somewhere, unfortunately i cant remember what it was :)

One solution is to recompile SDL.

I just clicked ahead a few posts from that link and this seems a little easier:

Quote:
 Actually, all that needs to be done is to recompile with "NO_STDIO_REDIRECT"defined in src/main/SDL_main.c (you can either modify the file or re-compileSDL_main.lib by hand with -DNO_STDIO_REDIRECT, your mileage may varry)

##### Share on other sites
Drew_Benton    1861
I cannot see why this is not working. Can you please post your code. It makes no sense that none of the suggestions have yet to work. What OS and IDE are you using? What version of SDL as well. My cout and printf work fine in Dev, VS6, and VS7, so there's one detail that is missing somewhere.

- Drew

##### Share on other sites
MrBeaner    144
Quote:
 Original post by OluseyiAdding Console I/O to a Win32 GUI App.

##### Share on other sites
Easca    163
Quote:
 Original post by Drew_BentonI cannot see why this is not working. Can you please post your code. It makes no sense that none of the suggestions have yet to work. What OS and IDE are you using? What version of SDL as well. My cout and printf work fine in Dev, VS6, and VS7, so there's one detail that is missing somewhere.- Drew

I don't get any output on the console. I didn't try the winapi redirects though.
I use dev-cpp 4.9.9.1 and SDL 1.2.4. The freopen works fine if I just put a arbituary filename and stops any output to stdout.txt using freopen( "CON", "wt", stdout ).

#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>/* The screen surface */SDL_Surface *screen = NULL;int main (int argc, char *argv[]){    int done;    printf( "Hello World1\n");    /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {        printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());        exit (1);    }    atexit (SDL_Quit);    freopen( "CON", "wt", stdout ); //hello world2 doesn't get outputted//    freopen( "filename", "wt", stdout ); //hello world2 goes to filename    printf( "hello world2\n");        exit( 0 );}

It is compiled as a console application (Win32 console).

##### Share on other sites
cignox1    735
Well, that's my code (just the basic stuff):
#include <stdio.h>#include <stdlib.h>#include "SDL.h"#include <iostream>using namespace std;/* The screen surface */SDL_Surface *screen = NULL;intmain (int argc, char *argv[]){    char *msg;    int done;    freopen( "CON", "wt", stdout );    freopen( "CON", "wt", stderr );    printf("ciccio\n");                  /* Initialize SDL */    if (SDL_Init (SDL_INIT_VIDEO) < 0)    {        sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());        free (msg);        exit (1);    }    atexit (SDL_Quit);    /* Set 640x480 16-bits video mode */    screen = SDL_SetVideoMode (640, 480, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);    if (screen == NULL)    {        sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n",          SDL_GetError ());        free (msg);        exit (2);    }        SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);    done = 0;    while (!done)    {        SDL_Event event;        /* Check for events */        while (SDL_PollEvent (&event))        {            switch (event.type)            {            case SDL_KEYDOWN:                break;            case SDL_QUIT:                done = 1;                break;            default:                break;            }        }    }    return 0;}

The Dev-C++ version is 4.9.9.1 with SDL version 1.2.4. Note that the program I tested was initially created by the Dev template, but I think that this makes no differences since parameters should be the same.

##### Share on other sites
Drew_Benton    1861
I will take a look at the code, but I am sure the problem lies in the fact you are using SDL 1.2.4. However, you are free to use it, but take a look at all the updates made! I would strongly suggest using at least version 1.2.7, but once again, you are free to use whatever you want. I know I have 1.2.8 and my console functions work just fine, cout and c-style alike. I'd say download a more recent version, but do not install over your current, simply make a test project, and move the files into that project and see if you get the same errors.

Instead of linking SDL.lib and such, use relative path names - ie
#pragma comment ( lib, "C:\\sdl 1.2.7\\lib\\sdl.lib" )

That's just an example of what you can do. I think it would be better for whatever you are doing because of the serious bugs that are fixed as of 1.2.7, but you can determine that for yourself.

- Drew

##### Share on other sites
cignox1    735
First of all thank you for having point out that there are more recent versions of sdl... I'm so lazy that I trusted that if it were a new version, it would have been released as package for Dev-C++ (sometimes I feel stupid :-( ((Uh, this sentences were too complex for my bad english: I hope they are at least understandable))

Second: it still doesn't work. I will try others configurations (don't know if #pragma used to link libraries is supported by Dev-C++, I thought that it was VC++ specific).

@Easca: could you tell me if the problem was solved on your system by updating SDL? (...if you want to update it, of course)

Thank you all anyway!

##### Share on other sites
Drew_Benton    1861
Ahh! [lol] I'm sorry, I got confused and thought that Easca was the OP. Ok for dev-cpp that #pragma will not work for the most part with SDL, so I would just copy the current version of SDL you have to a folder then install the new one. Then you can try your program again, since the linker libs will now be updated. Do not forget to update the .DLL in the system directory as well.

- Drew

##### Share on other sites
Easca    163
Ok at work I had sdl 1.2.4 I also assumed it would automatically give me the latest version through the package manager :-(.

Anyway at home I have SDl 1.2.7 installed and I also put in a copy of SDL 1.2.8 and both of them still print to stdout.txt.

##### Share on other sites
Drew_Benton    1861
Ok I'd like to say sorry for not trying out my suggestions first. I was using VS6 and VS7 the whole time and it worked fine. I am thinking that the only way to fix this under Dev-CPP and MingGW is by recompiling per the response to the link that Easca posted. I tried the redirecting output myself and could not get it to work either. I then tried Oluseyi's link as well, but it failed the same. As of now that is all I can think of.

- Drew

##### Share on other sites
Easca    163
Ah, well I'll just put up with the stdout.txt. It would be nice to be able to do it without recompiling though.

##### Share on other sites
cignox1    735
Ok. The funny thing is that I don't even get the stdout.txt file. Uhm... I will use some font library (I would have to use it however, soon or later).
Thank you again!

##### Share on other sites
Easca    163
Quote:
 Original post by cignox1Ok. The funny thing is that I don't even get the stdout.txt file. Uhm... I will use some font library (I would have to use it however, soon or later).Thank you again!

Did you try using:

freopen( "stdout.txt", "wt", stdout );
freopen( "stderr.txt", "wt", stderr );

##### Share on other sites
Rhaal    754
Quote:
 Original post by cignox1Ok. The funny thing is that I don't even get the stdout.txt file. Uhm... I will use some font library (I would have to use it however, soon or later).Thank you again!

Are you aware that the file deletes itself after your program ends? To make anything of it you'll most likely run your program in windowed mode and look at the text file during debugging/testing.

Edit: If nothing is written to it :)

[Edited by - Rhaal on February 10, 2005 5:43:36 PM]

##### Share on other sites
Drew_Benton    1861
Quote:
Original post by Rhaal
Quote:
 Original post by cignox1Ok. The funny thing is that I don't even get the stdout.txt file. Uhm... I will use some font library (I would have to use it however, soon or later).Thank you again!

Are you aware that the file deletes itself after your program ends? To make anything of it you'll most likely run your program in windowed mode and look at the text file during debugging/testing.

It should only do that when no text is sent there. If the file contains text, it is left intact.

##### Share on other sites
cignox1    735
Quote:
 freopen( "stdout.txt", "wt", stdout );freopen( "stderr.txt", "wt", stderr );

Uh, I thought that it would have been opened from the program without specific instructions...

## Create an account

Register a new account