• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# cout to console in SDL

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

41 replies to this topic

### #21cignox1  Members

Posted 10 February 2005 - 05:30 AM

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!

### #22Easca  Members

Posted 10 February 2005 - 07:36 AM

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 );

### #23Rhaal  Members

Posted 10 February 2005 - 08:43 AM

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]

### #24Drew_Benton  Members

Posted 10 February 2005 - 09:03 AM

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.

### #25cignox1  Members

Posted 10 February 2005 - 08:04 PM

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...

### #26Drew_Benton  Members

Posted 10 February 2005 - 08:24 PM

Quote:
Original post by cignox1
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...

No, your right. That is what SDL does auto. One last thing to try before I say throw in the towel. happybara's first post mentioned this, but maybe it was the use of the parameters that made it fail. Instead try using:
freopen( "CON", "w", stdout );

As you can see, there is no 't'. I got that idea from this MSDN Article. If that does not solve your problems, well then I'm sorry, I guess there is nothing you can really do. Best of luck!

- Drew

### #27cignox1  Members

Posted 11 February 2005 - 07:10 AM

Thank you again... but guess what? It doesn't work! I always hated console interfaces, I never liked to use them, and I always had to work with them... once that I need a simle output on a console, there is no way to make it work!
Allright, I will solve this problem in the future. First I have to being able to invert a matrix, but I already opened a topic about that.

### #28Arenth  Members

Posted 12 February 2005 - 05:10 PM

This might seem sort of obvious but use the freopen( "CON", "w", stdout ); call after sdl_init(); since SDL_init(); is the call that calls
freopens ("CON",w/e,"stdout.txt");.. It worked awesome for me....

Posted 13 February 2005 - 04:53 PM

added to the SDL FAQ since i see it a lot

http://www.libsdl.org/cgi/docwiki.cgi/FAQ_20Console

worked for you guys? AFTER SDL_Init?

### #30Drew_Benton  Members

Posted 13 February 2005 - 05:00 PM

Quote:
 Original post by Madsteradded to the SDL FAQ since i see it a lothttp://www.libsdl.org/cgi/docwiki.cgi/FAQ_20Consoleworked for you guys? AFTER SDL_Init?

It does not matter to me really since I am not a Dev-CPP user, but it did not work for me. I think it may be of the DevPak that I am using. I will try updating my SDL from a non-3rd party source.
#include <SDL/SDL.h>#include <cstdio>#include <cstdlib>#include <iostream>using namespace std;/* The screen surface */SDL_Surface *screen = NULL;int main(int argc, char *argv[] ){    char *msg;    int done;    // Initialize SDL //    if (SDL_Init (SDL_INIT_EVERYTHING) < 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);    }    freopen( "stdout.txt", "w", stdout );    freopen( "stderr.txt", "w", stderr );        cout << "Test" << endl;    printf("Test");        done = 0;    while (!done)    {        SDL_Event event;        // Check for events //        while (SDL_PollEvent (&event))        {            switch (event.type)            {            case SDL_KEYDOWN:                 if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }                 done = 1;                break;            case SDL_QUIT:                done = 1;                break;            default:                break;            }        }    }    return 0;}

 Yea it still doesn't work. Once I add SDL to a new project, the project stops working correctly. I do not know why this is happening. I will try one more thing though.

I even tried a reinstall of Dev, then I tried 1.2.7 first, then 1.2.8 and still couldn't get it to work. Sheesh what's going on. I guess it has to be something we are doing if it works for you guys fine.

[Edited by - Drew_Benton on February 13, 2005 11:00:05 PM]

### #31Easca  Members

Posted 13 February 2005 - 09:36 PM

Quote:
 Original post by ArenthThis might seem sort of obvious but use the freopen( "CON", "w", stdout ); call after sdl_init(); since SDL_init(); is the call that calls freopens ("CON",w/e,"stdout.txt");.. It worked awesome for me....

Is that using mingw/dev-cpp?

### #32happybara  Members

Posted 13 February 2005 - 11:51 PM

It seems that the freopen method works but you have to call the IMG_Load function at least once...

A working example
#include <iostream>#include <SDL/SDL.h>#include <SDL/SDL_opengl.h>#include <SDL/SDL_image.h>using namespace std;  SDL_Surface *screen;  int init_sdl(){     freopen( "CON", "w", stdout );    SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);int  videoflags= SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_OPENGL|SDL_ASYNCBLIT;   screen = SDL_SetVideoMode(300, 300 , 32, videoflags);	glViewport( 0, 0,300, 300 );};int main_loop(){   SDL_Event event;    Uint8 *keystate;   int  done=0;       while(!done)   {   	cout<<"1"<<endl;      glClearColor (0.7f, 0.9f, 0.9f, 1.0f);      glClear (GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);       SDL_GL_SwapBuffers( );          keystate = SDL_GetKeyState(NULL);            if ( keystate[SDLK_ESCAPE] ) {done=1;}      while ( SDL_PollEvent(&event) )      {         if ( event.type == SDL_QUIT )  {  done = 1;  }     } //while  } //while  return 0;}int dummy(){        SDL_Surface *img = IMG_Load("ab.bmp");};    int main(int argc, char *argv[]){      init_sdl();    main_loop();	  return 0;}

compiled as a 'win32 console' application
-lmingw32
-lopengl32
-lSDLmain
-lSDL
-lSDL_Image
-s
note that the dummy() function is not called.

Can anyone confirm that it works ?
Dolphins - The sharks of the sea.

### #33Easca  Members

Posted 14 February 2005 - 12:25 AM

That doesn't work for myself. :-(

### #34happybara  Members

Posted 14 February 2005 - 02:31 AM

I've just installed the newest version of SDL libraries and my previous code stopped working :/. But the following one does. Can you give it a try ?

#include <iostream>#include <fstream>#include <SDL/SDL.h>#include <SDL/SDL_opengl.h>using namespace std;  SDL_Surface *screen;  ofstream ctt("CON");  int init_sdl(){     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO);    freopen( "CON", "w", stdout );    freopen( "con", "w", stderr );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_OPENGL|SDL_ASYNCBLIT);    glViewport( 0, 0,300, 300 );    ctt.close();	};int main_loop(){   SDL_Event event;    Uint8 *keystate;   int  done=0;       while(!done)   {   	cout<<"cout"<<endl;   	cerr<<"cerr"<<endl;      glClearColor (0.7f, 0.9f, 0.9f, 1.0f);      glClear (GL_COLOR_BUFFER_BIT |  GL_DEPTH_BUFFER_BIT);       SDL_GL_SwapBuffers( );          keystate = SDL_GetKeyState(NULL);            if ( keystate[SDLK_ESCAPE] ) {done=1;}      while ( SDL_PollEvent(&event) )      {         if ( event.type == SDL_QUIT )  {  done = 1;  }     } //while  } //while  return 0;}  int main(int argc, char *argv[]){      init_sdl();    main_loop();	  return 0;}

Dolphins - The sharks of the sea.

### #35Easca  Members

Posted 14 February 2005 - 03:19 AM

That works for myself using [dev-cpp 4.9.9.1] and [SDL 1.2.4].

edit: C source-code

#include <SDL/SDL.h>#include <stdio.h>SDL_Surface *screen;FILE * ctt = fopen("CON", "w" );  int init_sdl(){     SDL_Init( SDL_INIT_VIDEO );    freopen( "CON", "w", stdout );    freopen( "con", "w", stderr );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF );    fclose( ctt );	};int main_loop(){	SDL_Event event; 	Uint8 *keystate;	int  done=0;	while(!done){		   		fprintf( stdout, "stdout\n" );		fprintf( stderr, "stderr\n" );		keystate = SDL_GetKeyState(NULL);  		if ( keystate[SDLK_ESCAPE] ){			done = 1;		}		while ( SDL_PollEvent(&event) ){			if ( event.type == SDL_QUIT ){				done = 1;			}		} //while	} //while	return 0;}  int main( int argc, char *argv[] ){	init_sdl();	main_loop();	return 0;}

Linker options: -lmingw32 -lSDLmain -lSDL -liberty -s

[Edited by - Easca on February 14, 2005 9:19:32 AM]

### #36Drew_Benton  Members

Posted 14 February 2005 - 04:09 AM

Quote:
 Original post by happybaraI've just installed the newest version of SDL libraries and my previous code stopped working :/. But the following one does. Can you give it a try ?*** Source Snippet Removed ***

That works for me too! As well as the C version Easca has put as well! I've figured out what does the trick, but it's not something I can understand. I did not use the same linker line, just the code.

Those two lines:
FILE * ctt = fopen("CON", "w" );fclose( ctt );

Are what made it work for me. I tried removing them and the program did not work as has been happening all along. Thanks to you two for posting that! Now my Dev-CPP works at least in console mode. This is something I will have to take a look into to see why this is a problem on Dev-CPP and not in Windows. Much appreciations for figuring this one out.

- Drew

"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

### #37Easca  Members

Posted 14 February 2005 - 04:42 AM

OK from a quick scan through some documents ( C++ reference: cstdio: freopen:, MSDN and the UNIX manual page for freopen) I noted this little snippet:

Quote:
 stream pointer to open file that has to be reopened.

So it appears that using devcpp/mingw the stdout/CON isn't opened so cannot be reopened. I'm guessing VC++ opens this file (CON) (and sdl or vc++ don't close it) allowing the freopen to work.

Another insignificant thing I noticed:

    FILE * ctt = fopen("CON", "w" );    foo = (int)freopen( "CON", "w", stdout );    bar = (int)freopen( "con", "w", stderr );    freopen( "stdout.txt", "w", stdout );    printf( "foo = %d, bar = %d, %d\n ", foo ,bar );    screen = SDL_SetVideoMode(300, 300 , 32, SDL_HWSURFACE|SDL_DOUBLEBUF );    fclose( ctt );

With the first line commented out "foo" and "bar" have the values 0 and 0 or NULL and NULL.

### #38Drew_Benton  Members

Posted 14 February 2005 - 05:39 AM

Quote:
Original post by Easca
OK from a quick scan through some documents ( C++ reference: cstdio: freopen:, MSDN and the UNIX manual page for freopen) I noted this little snippet:

Quote:
 stream pointer to open file that has to be reopened.

So it appears that using devcpp/mingw the stdout/CON isn't opened so cannot be reopened. I'm guessing VC++ opens this file (CON) (and sdl or vc++ don't close it) allowing the freopen to work.

Another insignificant thing I noticed:

*** Source Snippet Removed ***

With the first line commented out "foo" and "bar" have the values 0 and 0 or NULL and NULL.

Thank you for taking a look into that. However, I think the problem is not with devcpp/mingw, for my console works fine wit it. It's with SDL. Someone has made a mistake or something on the Dev-CPP release of SDL, so that it messed with those streams. I know this because I can start a new project and have everything work fine. As soon as I link in SDL though, everything goes FUBAR'd.

Thank you for your efforts in this manner! They are appreciated.

- Drew

"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

### #39cignox1  Members

Posted 14 February 2005 - 08:09 PM

...and this trick works for me too! Thank you all guys! Don't know why, but I felt that recompiling all was not the only solution, so I didn't do it ;-)

### #40amenzix  Members

Posted 24 March 2008 - 04:36 PM

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.