Undo a pipe. It is possible?

This is a discussion on Undo a pipe. It is possible? within the C Programming forums, part of the General Programming Boards category; Hi, I have this code where I put the standard output of my program from the screen to the pipe ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    52

    Post Undo a pipe. It is possible?

    Hi,

    I have this code where I put the standard output of my program from the screen to the pipe called "p4".

    Code:
    close(p4[0]);    
    close(1);
    dup(p4[1]);
    close(p4[1]);
    My question is, is there any way to undo this if later in the program I want to use again my screen as standard output?

    Thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    Use another dup() call to make a backup copy of whatever fd you want to go back to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Use another dup() call to make a backup copy of whatever fd you want to go back to.
    Thank you Salem so I make a dup(1) before the code in the first post and after this piece of code I make a close(1) to close the new standard output. Howevere, now screen is no longer standard output, now it's the pipe p4 so I can't just dup(1). What do I have to do now?

    I mean:

    Code:
    dup(1);
    close(p4[0]);       
    close(1);
    dup(p4[1]);
    close(p4[1]);	
    
    // Some code
    
    close(1);
    dup(?);

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    man page dup section 2
    heyThisIsMyBackupFD = dup(1);
    // blah
    dup(heyThisIsMyBackupFD);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    52
    Thank you it works!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 04:19 PM
  2. Replies: 4
    Last Post: 10-14-2009, 04:44 PM
  3. cont of IPC using PIPE
    By BMathis in forum C Programming
    Replies: 1
    Last Post: 03-15-2009, 05:16 PM
  4. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  5. Pipe(): Interprocess or Intraprocess comm?
    By @nthony in forum C Programming
    Replies: 2
    Last Post: 03-28-2007, 07:27 PM

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