Borland C++ reference

dup

raw OCR

Duplicates a file handle.

Defined in header <io.h>

Syntax

#include <io.h>
int dupCint handle);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

dup creates a new file handle that has the following in common with the original file handle:

Q same open file or device □ same file pointer (that is, changing the file pointer of one changes the other) Q same access mode (read, write, read/write)

handle is a file handle obtained from a _creat, creat, _open, open, dup, or dup2 call.

Return value

Upon successful completion, dup returns the new file handle, a non-negative in teger; otherwise, dup returns -1.

In the event of error, the global variable errno is set to one of the following:

EMFILE Too many open files EBADF Bad file number

See also

Example

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <io.h>

void flush(FILE  *stream);

int main (void)
{
   FILE *fp;
   char msg[] =  "This is a test";

   /* create a file  */
   fp = fopen ("DUMMY.FIL", "w");
   if  (fp) {
      /* write some data  to the file */
      fwrite(msg, strlen(msg) , 1, fp);
      clrscr 0 ;
      printf ("Press any key to flush DUMMY.FIL:");
      getchO ;
      /* flush the data  to DUMMY.FIL without closing it */
      flush(fp);
      printf ("\nFile was flushed, Press any key to quit:");
      getchO ;
   }
   else
      printf ("Error opening file!\n");
   return 0;

void flush (FILE *stream)
{
   int duphandle;

   /* flush EC's internal  buffer */
   fflush (stream);

   /* make a duplicate  file handle */
   duphandle = dup (fileno(stream));

   /* close duplicate handle  to flush DOS buffer */
   close (duphandle);

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.