Question about dup2

This is a discussion on Question about dup2 within the C Programming forums, part of the General Programming Boards category; I am wondering how to make dup2 to stop writing to a file even after i closed the file when ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    Question about dup2

    I am wondering how to make dup2 to stop writing to a file even after i closed the file when i use close(fd). For instance the last line is still writing to the file, but i want it to write to the console. How would i do this?

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    
    main()
    {
      int fd;
      char *s;
    
      fd = open("file4", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    
      if (dup2(fd, 1) < 0) { perror("dup2"); exit(1); }
    
      printf("Standard output now goes to file4\n");
    
      close(fd);
    
      printf("It goes even after we closed file descriptor &#37;d\n", fd);
    
      putchar('p');
      putchar('u');
      putchar('t');
      putchar('c');
      putchar('h');
      putchar('a');
      putchar('r');
      putchar(' ');
      putchar('w');
      putchar('o');
      putchar('r');
      putchar('k');
      putchar('s');
      putchar('\n');
    
      s = "And fwrite\n";
    
      fwrite(s, sizeof(char), strlen(s), stdout);
    
      fflush(stdout);
    
      s = "And write\n";
      write(1, s, strlen(s));
    
      printf("It goes even after we closed file descriptor %d\n", fd);
    }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    When you reopen standard output, it's not a new layer "on top" that will drop back to whatever standard output used to be when you close the new descriptor. Your standard output is now whatever you've got at file descriptor 1, no more, no less. Standard output isn't tied to the console in any way other than how your shell (or whatever called your program) decided to set things up.

    The good news is that all you have to do is save a copy of standard output before messing around with it:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main(void)
    {
      int fd, old_stdout;
      fd = open("file4", O_WRONLY | O_CREAT | O_TRUNC, 0666);
      old_stdout = dup(1);
      dup2(fd, 1);
      puts("Try number 1");
      fflush(stdout);
      close(fd);
      puts("Try number 2");
      fflush(stdout);
      dup2(old_stdout, 1);
      puts("Try number 3");
    
      return 0;
    }
    With any luck (because I did no error checking), file4 will contain the first two lines, and line 3 will wind up back at the original standard output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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