redirecting a child process output to a file

Bill Ives wei at hpctdls.HP.COM
Tue Jun 20 00:46:03 AEST 1989



    To redirect a child's stdout to a file in the way you
    described does not work because the child process does
    not parse the ">" out of the command line.  When the
    redirection operators are used they are used on the DOS
    command line  --- where DOS ( namely command.com ) parses
    them out and determines that redirection is to be done. The
    same thing works for pipes "|".  Since the command.com shell
    does this parsing and redirection you may invoke it with the
    child process name and args following like:
    
    c:\command.com  child.exe > outfile.dat

    You should get the command.com specification out of your environment
    by looking at the "COMSPEC=" environment variable.

    You could also do the redirection yourself by using DOS functions
    DOS_DUP_HANDLE, DOS_CLOSE  ....  The idea in using these is to do
    the same thing the command shell does when it sees the ">" operator.
    The basic algorithm for doing this is:

    duplicate your stdout and save the duplicate in a temp variable
    open the child's stdout file
    close your stdout handle so the that it may be attached to the
       file opened above.
    use DOS_DUP again to attach the opened file to the stdout

    your stdout is now the file that was opened.

    invoke the child -- it will inherit the stdout that
         is attached to the file.

    restore your own stdout by closing stdout and duping the
       temp handle saved in the first step.  Your stdout will
       then be restored.



More information about the Comp.lang.c mailing list