Thread: How to reopen stdout?

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    50

    How to reopen stdout?

    Look:
    Code:
    int main()
    {
        FILE *stream,*fp ;
        if((stream = freopen("temp","w",stdout)) == NULL)
        {
           perror("fopen temp error");
           return -1;
         }
        execl("/bin/ls","ls","-l",0);
    
        freopen("/dev/tty","w",stdout);  
        printf("And now back to the console once again\n");
        fclose(stream);
        return 0;
    }
    I find this statement printf("And now back to the console once again\n")
    sometimes it can run,sometimes it cann't run,why?How to correct?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Let's make sure we're on the same page here. What do you expect from the following code?
    Code:
    #include <unistd.h>
    #include <stdio.h>
    int main(void) {
        execl("/bin/ls", "ls", "-l", 0);
        printf("I'm the Cheshire cat!\n");
        return 0;
    }
    Is that the output you actually get?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by leetow2003 View Post
    Look:
    Code:
    int main()
    {
        FILE *stream,*fp ;
        if((stream = freopen("temp","w",stdout)) == NULL)
        {
           perror("fopen temp error");
           return -1;
         }
        execl("/bin/ls","ls","-l",0);
    
        freopen("/dev/tty","w",stdout);  
        printf("And now back to the console once again\n");
        fclose(stream);
        return 0;
    }
    I find this statement printf("And now back to the console once again\n")
    sometimes it can run,sometimes it cann't run,why?How to correct?
    If execl function succeeded then nothing will be executed after statement
    Code:
        execl("/bin/ls","ls","-l",0);
    Only if execl failed for any reason than
    Code:
        freopen("/dev/tty","w",stdout);  
        printf("And now back to the console once again\n");
        fclose(stream);
        return 0;
    will be executed.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    50
    Quote Originally Posted by tabstop View Post
    Let's make sure we're on the same page here. What do you expect from the following code?
    Code:
    #include <unistd.h>
    #include <stdio.h>
    int main(void) {
        execl("/bin/ls", "ls", "-l", 0);
        printf("I'm the Cheshire cat!\n");
        return 0;
    }
    Is that the output you actually get?
    Because I want to write to the file via stdout,so I don't use function execl

  5. #5
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    I'm not sure if you caught what tabstop was saying. When you call execl() (and it succeeds, of course) then your program in memory gets completely replaced with the new one. So any code that occurs after it, won't get executed, ever, at all; not even once that new program finishes running.
    For this, fork() first, or use system() instead. (See their respective manual pages for more info)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 07-14-2011, 10:08 AM
  2. stdout -> ram?
    By Verdagon in forum C++ Programming
    Replies: 10
    Last Post: 04-26-2011, 12:41 AM
  3. stdout with JNI
    By GoLuM83 in forum C Programming
    Replies: 4
    Last Post: 12-14-2006, 01:27 PM
  4. using stdout
    By rebok in forum C Programming
    Replies: 4
    Last Post: 11-05-2004, 06:19 PM
  5. HACKING! (reopen)
    By dayknight in forum A Brief History of Cprogramming.com
    Replies: 41
    Last Post: 05-08-2002, 05:53 PM