redirecting a child process output to a file

Warlow jamespw at mqcomp.oz
Mon Aug 7 10:57:11 AEST 1989


In article <8430 at techunix.BITNET> buzy%techunix.bitnet at jade.berkeley.edu (boaz binnun) writes:
>I need to redirect the output of a child process to a file, ...
>what  I did was :
>
>char *args[] = { "child.exe", "argument", ">", "filename", NULL   };
>
>and than:
>
>spawnl(P_WAIT,args[0],args[0],args[1],args[2],args[3],args[4]);

This won't work because the indirection operator > is interpreted by the
shell, NOT by C. spawnl doesn't invoke the shell so the file child.exe
sees the argument '>', which will probably frighten it. If you *really*
want to use indirection, something like
	system("child.exe argument > filename");
will do the trick (but it's awful); otherwise, use fork to explicitly
reset stdout yourself:
	if (fork() == 0) {
		/* child process */
		stdout = freopen("filename", "w", stdout);
		execl(args[0],args[0],args[1],args[4]);
	}
or some such.



More information about the Comp.lang.c mailing list