View Full Version : How to modifythe standard output (screen) into my own output (file)

06-06-2002, 01:15 AM
I'm new in OpenGL and I need to know how I can change the standard output (screen) into my output (file) and if can use any extension for the file.

Could you help me?

06-06-2002, 03:17 AM
You mean a screen print/dump?

06-06-2002, 05:02 AM
Yes! You can redirect stdout to a file. I'll tell you how but the problem is I don't know how to redirect stdout back to the screen so once you've done this your program won't be able to write to the screen again unless you can redirect stdout back to the screen. If you figure out how to do that let me know...

FILE *NewOut;
NewOut = fopen("filename.ext","w");

The idea is when a request for a file handle is made the lowest handle is returned. Usually this is a value of 7 or above. stdout I think had a value of 2 so by closing stdout, the next file you open will recive the lowest available handle, 2, making it the new stdout. Everything written to file handle 2 will go to what ever file you opened after closing stdout. But like I said, I don't know how to reverse the process... let me know if you figure out how.

06-06-2002, 08:13 AM
on un*x systems you can use dup2(). it copies one file descriptor to another. like this:

FILE *fp = fopen("filename", "w");
if (fp)
dup2(2, fp);

now, when anything gets written to 2 (stdout), it also gets written to fp. not sure if there is a windows equivalent.


06-06-2002, 09:52 PM
MSDN actually has this documented pretty well. Look up "low level i/o", or something similar. It looks like dup2 does exist on Windows (though I've never tried it on anything but *nix).

(BTW - hate to get nitpicky, but stdout is really file descriptor 1... stderr is file descriptor 2. http://www.opengl.org/discussion_boards/ubb/smile.gif )

Wouldn't it just be sufficient to redirect the output to a file though? For example, if your program is called 'myprog', and you want to redirect it to the file 'out.txt', simply do:

myprog > out.txt

Then you don't even have to worry about getting all of the file descriptors right.