Path: utzoo!attcan!uunet!samsung!sdd.hp.com!uakari.primate.wisc.edu!aplcen!mab718 From: mab718@aplcen.apl.jhu.edu (Bellmore Mark Allen) Newsgroups: comp.lang.c Subject: redirecting stdout and stdin Summary: Using pipe and dup to redirect stdout and stdin Keywords: stdin stdout Message-ID: <5905@aplcen.apl.jhu.edu> Date: 8 Jul 90 15:25:39 GMT Distribution: comp.lang.c Organization: Johns Hopkins University, Laurel, MD Lines: 33 I have seen a few questions reguarding redirection of stdin and stdout. I apologize if this has been said before. A good reference is Advanced UNIX programming by Marc J. Rochkind, published by PRENTICE-HALL 1985. Here is some sample code: #include main() { int pfd[2]; char buf[10]; /* pipe makes pfd[0] for reading and pfd[1] for writing */ if(pipe(pfd) == -1) { perror("pipe"); exit(-1); } /* now close stdin to make it available */ close(0); /* dup is GUARANTEED to return the lowest availble fd */ /* so doing a dup will copy pfd[1] to stdin (fd 1) */ dup(pfd[0]); close(pfd[0]); /* don't need it anymore */ /* now reading from stdin will come from the pipe */ strcpy(buf,"TEST PIPE"); write(fd,buf,9); read(0,inbuf,9); } /* This example is just to show how to redirect stdin */ /* The same can be done with stdout */