Advertisement

Using printf() in Windows programs

Started by November 18, 2003 11:52 AM
9 comments, last by tomdavis 21 years, 3 months ago
Hello, I''m trying to program a 3D game using Nehe''s tutorials as a basis. I''ve been hitting a few bugs and would really like to stick a couple of printf()''s in to output variable values and so on. I can''t work out how to get hold of this output-- is there any way to make a console appear with this output in it. I''m using MS Visual C++ to develop my program, does this provide any features like this? Thanks in advance, any help would be much appreciated, Tom Davis
Use ofstreams instead.

#include <fstream>using namespace std;main(){  ofstream out("myfile.txt"); //presumes ASCII mode  out << "error!" << endl;  out.close(); }


Reason? In Windows, anything you output to the console needs to be redirected and that implies a whole mess of things you need to do - it's easier to use streams and output stuff to files. Just keep ind mind that the OS has a symbolic limit on the number of files at any given time.

edit: added "at any given time"







[edited by - crispy on November 18, 2003 3:05:53 PM]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
Nice 1 Crispy ive been writing an app for a while and have had to have a console app running along side it to produce output for debugging but you just saved me lots of effort
Excellent, thanks for the help. :-)

One other quick question-- is there an equivalent of the "tail -f " *NIX command? So I can keep an eye on what''s being written to the file in realtime (rather than after execution has finished).

Thanks,

Tom
Popping a console with a gui based program isn''t difficult.

To pop a console and setup stdout:

AllocConsole();
freopen("CONOUT$", "wb", stdout);

To clean up afterward:

fclose(stdout);
FreeConsole();

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
Also even if you set up a Console app, you can still create windows and do pretty much anything you can in a Windows app. Just have a traditional main function instead of a WinMain one.

Your instance handle can be found using GetModuleHandle.


Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
Advertisement
Thank you LessBread, this is perfect. :-) No fuss, nice and simple. Now to fix the bug in my camera functions.

Cheers,
Tom
Should this work with a Win32 console application using glut ?
I tried ; it compiles fine, but nothing happens ???
Just try a printf("toto\n"); and enjoy

- Iliak -
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
..... thinking ...

what kind of console window is it ? Window style ? Or am''I
trying to open the console that already exists (dos style) :D ?


This topic is closed to new replies.

Advertisement