Borland C++ reference

fdopen

raw OCR

Associates a stream with a file handle.

Defined in header <stdio.h>

Syntax

#include <stdio.h>
FILE *fdopen(int handle, char *type);

Portability

DOSUNIXWindowsANSI CC++ only
■■■

Remarks

fdopen associates a stream with a file handle obtained from creat, dup, dup2, or open. The type of stream must match the mode of the open handle.

The type string used in a call to fdopen is one of the following values:

r Open for reading only.

w Create for writing.

a Append; open for writing at end-of-file or create for writing if the file does not exist.

r+ Open an existing file for update (reading and writing).

ZV+ Create a new file for update.

a+ Open for append; open (or create if the file does not exist) for update at the end of the file.

To specify that a given file is being opened or created in text mode, append a t to the value of the type string (rt, w+t, and so on); similarly, to specify binary mode, append a & to the type string {zvb, a+b, and so on).

If a f or b is not given in the type string, the mode is governed by the

global variable _fmode. If _fmode is set to O_BINARY,files will be opened

in binary mode. If _fmode is set to O_TEXT, they will be opened in text mode. These O ... constants are defined in fcntl.h.

When a file is opened for update, both input and output can be done on the resulting stream. However, output cannot be directly followed by input without an intervening fseek or rewind, and input cannot be directly followed by output without an intervening fseek, rewind, or an input that encounters end-of-file.

Return value

On successful completion, fdopen returns a pointer to the newly opened stream. In the event of error, it returns null.

See also

Example

#include <sys\stat.h>
iinclude <stdio.h>
#include <fcntl.h>
#include <io.h>

int' main(void)
int handle;
FILE *stream;
/* open a file  */
handle = open ("DUMMY.FIL", 0_CREAT, S_IREAD  I S_IWRITE);

/* now turn the handle  into a stream */
stream = fdopen (handle, "w");
if  (stream == NULL)
   printf( "fdopen failed\n");
else {
   fprintf (stream, "Hello world\n");
   fclose(stream) ;
}
return 0;

Differences from modern implementations

Nothing recorded yet.

Modern references

These are search links, not yet checked by hand.