The problem is, none of us really have time to write up the several hundred word response that would completely describe how to do that.
Originally Posted by khdani
Here's some pseudocode, but I'm not going to fill in all the blanks
In other words, the shell opens the output file. Then it forks itself to create a child. The child closes its stdout descriptor, and calls dup() to replace that with the fd of the open file. Then it executes the ls program. The parent, meanwhile, closes the fd (since it doesn't need it) and waits for the child to finish running.
fd = open("out.txt")
if fork() == parent:
wait for the child
If you want more details, I'll trade you -- provide some code.