Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!godot!harvard!seismo!brl-tgr!tgr!hadron!jsdy@SEISMO.ARPA From: jsdy@SEISMO.ARPA Newsgroups: net.lang.c Subject: Re: Redirect output file descriptor 3 (not just 1 and 2) Message-ID: <6966@brl-tgr.ARPA> Date: Fri, 4-Jan-85 00:11:55 EST Article-I.D.: brl-tgr.6966 Posted: Fri Jan 4 00:11:55 1985 Date-Received: Sat, 5-Jan-85 02:13:55 EST Sender: news@brl-tgr.ARPA Organization: Ballistic Research Lab Lines: 63 > how can I program the following: > program > output 2> error 3> my_file This is not a good way to do what it seems you want to do. If you really want to, though, try the following: -------------------- #include#include #include #define FDES 3 #define ROPEN 0 #define WOPEN 1 #define PRVMOD 0600 char file[] = ".cshrc"; main() { register int i, j; struct stat sb; if (fstat(FDES, &sb) < 0) { j = i = open(file, WOPEN); if (i < 0) j = i = creat(file, PRVMOD); if (i >= 0 && i != FDES) { j = dup2(i, FDES); close(i); } printf("Opened <%s> on %d (%d)\n", file, j, i); } else printf("File type is 0%o\n", sb.st_mode); /* Put the UNIX file descriptor into a stdio structure. */ /* Rest of code */; } -------------------- A better way to program this would be: -------------------- [preamble] main(argc, argv, envp) int argc; char **argv; char **envp; { register char *fp = file; register FILE *fstream; if (--argc > 0) fp = *++argv; fstream = fopen(fp, WRITE); /* Rest of code */; } -------------------- This is called as 'program ...' or 'program ... my_file', depending on whether you want it to use the default file or your special file. Note that you don't have to depend on a non-stdio construct like dup2, or UNIX's reliability in opening file descriptors serially, or anything like that. Joe Yao (UUCP!seismo!hadron!jsdy / hadron!jsdy@seismo.ARPA)