Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <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 a previously opened file
    associated with 'handle1'.  Because all handles affiliated with a
    file use the same file pointer, either file handle can then be used
    to carry out operations on the file.


    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 ('stdout').

            #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() dup() open()

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