#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    clsoing standardout


    Hi I am trying to close the standardoutfile stream using close then open a file. Then I want to close the file, and reopen stdout. How? Thanks
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Are you trying to redirect stdout to a file?
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I haven't played with that myself and my book that covers it is at home. But here's some code I found online that might cover what you're trying to do:
    Code:
    static int
    _rmt_rexec (char *host, char *user)
    {
      int saved_stdin = dup (STDIN_FILENO);
      int saved_stdout = dup (STDOUT_FILENO);
      struct servent *rexecserv;
      int result;
    
      /* When using cpio -o < filename, stdin is no longer the tty.  But the
         rexec subroutine reads the login and the passwd on stdin, to allow
         remote execution of the command.  So, reopen stdin and stdout on
         /dev/tty before the rexec and give them back their original value
         after.  */
    
      if (freopen ("/dev/tty", "r", stdin) == NULL)
        freopen ("/dev/null", "r", stdin);
      if (freopen ("/dev/tty", "w", stdout) == NULL)
        freopen ("/dev/null", "w", stdout);
    
      if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
        error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));
    
      result = rexec (&host, rexecserv->s_port, user, NULL,
    		   "/etc/rmt", (int *) NULL);
      if (fclose (stdin) == EOF)
        error (0, errno, _("stdin"));
      fdopen (saved_stdin, "r");
      if (fclose (stdout) == EOF)
        error (0, errno, _("stdout"));
      fdopen (saved_stdout, "w");
    
      return result;
    }
    The dup() calls are low-level I/O, as is fdopen().

    Hope that helps.

IMN logo majestic logo threadwatch logo seochat tools logo