FDOPEN - associate stream with file descriptor.

(Slightly different from UNIX function)

Usage:

#include <stdio.h>
#include <fildes.h>
FILE *fdopen();
fp = fdopen(fildes,options);

Where:

int fildes;
is a file descriptor of the sort returned by the "open" or "creat" functions.
char *options;
are options similar to those used for the "fopen" function. These are described below.
FILE *fp;
points to a control block associated with the stream. "fp" is used in I/O operations on the stream, e.g. "fprintf", "fscanf", "fclose", etc. If "fdopen" detects an error, "fp" will be the NULL pointer.

Description:

The "fdopen" function is the link between the file descriptor I/O routines ("open", "close", "read", "write") and the stream I/O routines ("fopen", "fclose", "fread", "fwrite"). "fdopen" takes a file descriptor as an argument and returns an appropriate stream file pointer.

"fdopen" does not actually open a file -- you should already have done that with "open" or "creat". However, the options to "fdopen" are the same options used in opening a file with "fopen" because this information is needed to set up the control block for the file. This means that the "options" string for "fdopen" can be any of the following.

r
unit was opened for reading.
w
unit was opened for writing.
a
unit was opened for appending.
r+
unit was opened for both reading and writing. The stream will be positioned at the beginning of the file.
w+
unit was opened for both reading and writing. The stream will be created if it does not exist, and will be truncated if it does exist.
a+
unit was opened for both reading and writing. The stream will be positioned at the end of the existing file content.
rb
unit was opened for reading. The 'b' indicates binary data (as opposed to text).
wb
unit was opened for writing. The 'b' indicates binary data.
ab
unit was opened for appending. The 'b' indicates binary data.

You will receive an error if the options for "fdopen" conflict with the options specified when the unit was actually opened. For example, if the unit was opened with "open" using the read-only option, you will get an error if you specify "w" as the option to "fdopen".

See Also:

expl c lib open

expl c lib fopen

expl c include fildes

Copyright © 1996, Thinkage Ltd.