How to reopen stdout?

This is a discussion on How to reopen stdout? within the Linux Programming forums, part of the Platform Specific Boards category; Look: Code: int main() { FILE *stream,*fp ; if((stream = freopen("temp","w",stdout)) == NULL) { perror("fopen temp error"); return -1; } ...

  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,185
    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
    1,691
    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, 11:08 AM
  2. stdout -> ram?
    By Verdagon in forum C++ Programming
    Replies: 10
    Last Post: 04-26-2011, 01: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, 06:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21