home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11603 < prev    next >
Encoding:
Text File  |  1992-07-25  |  2.2 KB  |  78 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!curt
  3. From: curt@watson.ibm.com (Curt McDowell)
  4. Subject: Re: redirecting stdout
  5. Sender: news@watson.ibm.com (NNTP News Poster)
  6. Message-ID: <1992Jul25.232634.7251@watson.ibm.com>
  7. Date: Sat, 25 Jul 1992 23:26:34 GMT
  8. Lines: 61
  9. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  10. References:  <1992Jul25.184724.27204@news.acns.nwu.edu>
  11. Nntp-Posting-Host: gorby.watson.ibm.com
  12. Organization: IBM T.J. Watson Research Center
  13. Keywords: stdout, redirect
  14.  
  15. In article <1992Jul25.184724.27204@news.acns.nwu.edu>, tindall@casbah.acns.nwu.edu (Mike Tindall) writes:
  16. >     How can  I return stdout to printing to the screen after  redirecting
  17. > it to a file using out_file=freopen("fname","w",stdout)?
  18.  
  19. The following works for me.  I think it's reasonably robust, but I've only
  20. tested it using the example main() program below.
  21.  
  22. ----------------------------------------------------------------------------
  23.  
  24. #include <stdio.h>
  25.  
  26. /*
  27.  * redirect_stdout                        by Curt McDowell, IBM 07-25-92
  28.  *
  29.  *   Redirects stdout to a file in a fairly idiot-proof way.  Returns 0
  30.  *   unless there is an error writing the specified file, in which case
  31.  *   it returns -1, sets errno, and leaves stdout alone.  If filename is
  32.  *   NULL, stdout is restored to the real stdout.
  33.  */
  34.  
  35. int redirect_stdout(filename)
  36.     char           *filename;
  37. {
  38.     static    int    ofd    = -1;
  39.  
  40.     if (filename) {
  41.         if (ofd < 0 && (ofd = dup(1)) < 0)
  42.             return -1;
  43.         if (freopen(filename, "w", stdout) == 0) {
  44.             redirect_stdout(0);
  45.             return -1;
  46.         }
  47.     } else if (ofd >= 0) {
  48.         freopen("/dev/null", "w", stdout);
  49.         dup2(ofd, 1);    /* Sneak in under stdio */
  50.         close(ofd);
  51.         ofd = -1;
  52.         setlinebuf(stdout);
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58. main()
  59. {
  60.     printf("This goes to the screen\n");
  61.  
  62.     redirect_stdout("file1");
  63.  
  64.     printf("This goes to file1\n");
  65.  
  66.     redirect_stdout("file2");
  67.  
  68.     printf("This goes to file2\n");
  69.  
  70.     redirect_stdout(0);
  71.  
  72.     printf("This goes back to the screen\n");
  73. }
  74.  
  75. ----------------------------------------------------------------------------
  76. Curt McDowell                             IBM and I disclaim anything that
  77. IBM T. J. Watson Research Center          happens as a result of this post.
  78.