Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Turbo C - <b>dup2() reassign a file handle</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
dup2()                   Reassign a File Handle

 #include <io.h>                         Required for declarations only

 int        dup2(handle1, handle2);
 int        handle1;                     Handle referring to open file
 int        handle2;                     Any handle value

    dup2() causes 'handle2' to refer to the same file as 'handle1', which
    is an already open file.  Either file handle can be used to carry out
    operations on the file, since all handles associated with a given file
    use the same file pointer.


    Returns:    Zero if successful getting a new file handle,  or -1 if
                there is an error. On error, ERRNO is set to:

                    EBADF         Invalid file handle
                    EMFILE        Too many files open

      Notes:    In the example below, if file handle 3 is already open, it
                will be closed first.

  -------------------------------- Example ---------------------------------

    These statements make file handle 3 refer to the same file as file
    handle 1.

          #include <io.h>
          #include <stdlib.h>

          int  fh;

          main()
          {
                 fh = dup2(1,3);
                 if (fh == -1)
                    perror("error setting duplicate file handles");
           }

See Also: close() creat() open()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson