Redirecting printf()? stdout accutually?

Started by
4 comments, last by DvDmanDT 18 years, 8 months ago
Hello everyone.. I know you can redirect stdout to a file with reopen().. My question is if you can redirect it to say, sockets, an ingame console (like FPS games have) or something else? Is it possible? And if so, how?
Advertisement
Nope. You might be able to do something in Linux with low level files (open() close(), etc), because in Linux, sockets and files are handled the same way. I'm not really sure about anything with low level files though.

I'm almost certain you can't redirect to a in-game console or socket in WIndows though. Your best bet would be to write another layer on top of printf(), and use varg lists like printf does.
I think I did manage to get stdout redirected once using VB.. Like.. GetStandardOutput or something.. Hmm.. You'd think it should be possible.. :p
There is a tool though that does that sort of redirection called netcat. You simply pipe your command into netcat [and provide it some parameters] and netcat sends everything it hears on stdin to whatever parameters you specify.

I'm fairly certain there's a windows port of it.

Though personally, I just use stringstream and send the result to my quake-style console or networking layer as the pre-made string. Others use boost::format, or lexical_cast to format/stringify their data.
With Windows at least, you can get and set the standard input, output and error handles with the GetStdHandle() and SetStdHandle().
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Hmm.. How about the CreateFile() function, in combination with SetStdHandle(), to redirect it to a temporary buffer, then every now and then check it and send/output it however you may wish? Would that work?

This topic is closed to new replies.

Advertisement