Click to See Complete Forum and Search --> : redirecting stdin and stdout programatically
Get_Free
July 24th, 2005, 03:00 PM
In order to redirect stdin and stdout in a shell environment I just do this:
./exefile < file.in >file.out
But how can I do this programatically, inside the program, that is, that a program redirect itself its own stdin and stdout??
thanx.
SuperKoko
July 24th, 2005, 04:02 PM
There is no standard way, but OS specific ways.
Under Win32, you can use
SetStdHandle (http://msdn.microsoft.com/library/en-us/dllproc/base/setstdhandle.asp)
Or, when you launch another process, CreateProcess has a STARTUPINFO structure containing a flag, and fields for redirecting stdout, stdin, and stderr.
earl
July 24th, 2005, 08:30 PM
If we're talking UNIX, look into the dup2() function. You can duplicate open file descriptors (file.in and file.out) across the stdin and stdout descriptors (0 and 1, or STDIN_FILENO and STDOUT_FILENO).
Improving
July 24th, 2005, 09:27 PM
It can be done standardly (http://www.cuj.com/documents/s=7997/cujcexp1903hyslop/)
Get_Free
July 24th, 2005, 10:22 PM
It can be done standardly (http://www.cuj.com/documents/s=7997/cujcexp1903hyslop/)
hehe, just that simple, assign a new stream to cin, cout and cerr :)
Improving
July 24th, 2005, 11:14 PM
read it all.
Get_Free
July 25th, 2005, 02:28 PM
read it all.
Oh, my mistake.
anyway... it can be done, that's the good thing
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.