Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with 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 associated with a previously opened file.  Either file handle can
    be used to carry out operations on the file, because both handles use
    the same file pointer.


       Returns:     0, if successful. On error, -1 is returned and
                    '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() dup()

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