Article View: comp.os.msdos.programmer
Article #5658Re: Redirecting stdout via dup2
From: tom milner
Date: Wed, 03 Apr 1991 17:15
Date: Wed, 03 Apr 1991 17:15
43 lines
2074 bytes
2074 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
You were real close (but no cigar). The line after "undo redirection to stdout"
should read:
dup2(oldout, 1);
And that will do it.
-Tom.
Technical Details
Message-ID:
<27580001@hprpcd.rose.hp.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpda!hpcupt1!hprnd!hprpcd!tmilnerReferences:
<970@masterCNA.TEK.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpda!hpcupt1!hprnd!hprpcd!tmilner From: tmilner@hprpcd.rose.hp.com (tom milner) Newsgroups: comp.os.msdos.programmer Subject: Re: Redirecting stdout via dup2 Message-ID: <27580001@hprpcd.rose.hp.com> Date: 3 Apr 91 17:15:11 GMT References: <970@masterCNA.TEK.COM> Organization: Performance Technology Center, Roseville Lines: 43