redirecting a child process output to a file

Billy G. Allie bga at bgalli.eds.com
Tue Jun 20 12:22:51 AEST 1989


In article <8430 at techunix.BITNET>, buzy at techunix.BITNET (boaz binnun) writes:
> I need to redirect the output of a child process to a file,
> I am working with Turbo C, what  I did was :
> 
> 	char *args[] = { "child.exe", "argument", ">", "filename", NULL };
> 	spawnl(P_WAIT,args[0],args[0],args[1],args[2],args[3],args[4]);
> 
> and it didnt work,does anyone know why ?

The redirection of the standard input and output files using the '>' and '<'
characters is a function the shell (command.com in this case).  Executing a
program with spawn or exec with the redirection character(s) in the
argument list will not cause redirection (unless the program is command.com
or the prgram has been written to recognize the redirection characters as
such).  To redirect the output of a child process to a file you can do one
of two things:

1.   Use the system function, which will use command.com to execute the
     program and set up the redirection.  For example:

     system("child argument >filename");

     The disadvantage to this method is that another copy of command.com
     must be loaded into memory in addition to the child program.  Also,
     there is a bug in command.com that prevents the exit code from the
     child process from returning correctly.  The advantages are that it
     is easy to use and even pipes can be set up in this manner.

2.   Set up the file redirection yourself before spawning the child
     process.  This can be done as follows:

     int oldstdout, newstdout;
	...
     oldstdout = dup(2);	/* Save the original stdout handle	    */
     newstdout = open("filename", O_CREAT|O_TRUNC|O_WRONLY); /* open output */
     dup2(newstdout, 2);	/* Make the new file the standard output    */
     close(newstdout);		/* Close the (now) un-needed file handle    */
     spawnl(P_WAIT, "child.exe", "child", "argument"); /* Execute the child */
     dup2(oldstdout, 2);	/* Restore the original stdout handle	    */
     close(oldstdout);		/* Close the un-needed file handle	    */

     Please note that I did not include any code to check the return
     values of the dup, dup2, close, open and spawnl function calls.
     Those checks will have to be added for the code sequence to be
     truly useful.

     The disadvantage of this method is that it is harder to set up.
     The advantages are it can be used to redirect any of the 5 standard
     devices used in the MS-DOS environment (stdin, stdout, stderr,
     stdaux and stdprn) and it does not require any additional processes
     to be loaded (i.e. command.com).

I hope this helps you with your problem.
-- 
____	   | Billy G. Allie	| Internet..: bga at bgalli.eds.com
|  /|	   | 7436 Hartwell	| UUCP......: uunet!{mcf|edsews}!bgalli!bga
|-/-|----- | Dearborn, MI 48126	| Compuserve: 76337,2061
|/  |LLIE  | (313) 582-1540	| Genie.....: BGALLIE



More information about the Comp.lang.c mailing list