Article View: comp.os.msdos.programmer
Article #5372Redirecting stdout via dup2
From: Wayne Turner
Date: Fri, 22 Mar 1991 00:58
Date: Fri, 22 Mar 1991 00:58
34 lines
1886 bytes
1886 bytes
I have an application where I want to redirect stdout to a file under
program control (not from the command line). I'm using dup and dup2 to
achieve this and it works with the exception that I can't *undo* the
redirection within the program (however, exiting to command.com *does*
result in undoing the redirection).
The following code fragment shows what I'm doing; each comment line shows
the intent and result of the line it precedes.
/* dup stdout and redirect it to temp.fil */
oldout = dup(1);
newout = open("temp.fil", O_WRONLY|O_CREAT, 0777);
dup2(newout, 1);
/* write to 1 -- output redirected to temp.fil */
write(1, temp_file_string, strlen(temp_file_string));
/* write to dup'd stdout -- output goes to console */
write(oldout, stdout_string1, strlen(stdout_string1));
/* undo redirection on stdout */
dup2(1, oldout);
/* string2 should go to console but goes to temp.fil instead */
write(1, stdout_string2, strlen(stdout_string2));
I've tried the same with direct DOS calls (INT 21 Function 45H (dup)
and INT 21 Function 46H (dup2)) but get the same results. I've
tried many combinations of close()'ing handles before and after the
second dup2 but none gives the desired effect. (BTW, I'm using DOS 3.31)
Any ideas/solutions welcome.
Thanks,
Wayne Turner
Tektronix, Inc.
Redmond, Oregon
waynet@kit.CNA.TEK.COM
Technical Details
Message-ID:
<970@masterCNA.TEK.COM>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!att!bellcore!uunet!zephyr.ens.tek.com!master!kit!waynetShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!pacbell.com!att!bellcore!uunet!zephyr.ens.tek.com!master!kit!waynet From: waynet@kit.tek.com (Wayne Turner) Newsgroups: comp.os.msdos.programmer Subject: Redirecting stdout via dup2 Message-ID: <970@masterCNA.TEK.COM> Date: 22 Mar 91 00:58:38 GMT Sender: news@masterCNA.TEK.COM Reply-To: waynet@kit.tek.com (Wayne Turner) Organization: Tektronix, Inc., Redmond, Oregon Lines: 34