### fdopen -- Associates Input or Output With File

Format

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

Language Level: XPG4, Extension
fdopen associates an input or output stream with the file identified by handle. The type variable is a character string specifying the type of access requested for the stream.

Mode | Description
r | Create a stream to read a text file. The file pointer is set to the beginning of the file.
w | Create a stream to write to a text file. The file pointer is set to the beginning of the file.
a | Create a stream to write, in append mode, at the end of the text file. The file pointer is set to the end of the file.
r+ | Create a stream for reading and writing a text file. The file pointer is set to the beginning of the file.
w+ | Create a stream for reading and writing a text file. The file pointer is set to the beginning of the file.
a+ | Create a stream for reading or writing, in append mode, at the end of the text file. The file pointer is set to the end of the file.
rb | Create a stream to read a binary file. The file pointer is set to the beginning of the file.
wb | Create a stream to write to a binary file. The file pointer is set to the beginning of the file.
ab | Create a stream to write to a binary file in append mode. The file pointer is set to the end of the file.
r+b or rb+ | Create a stream for reading and writing a binary file. The file pointer is set to the beginning of the file.
w+b or wb+ | Create a stream for reading and writing a binary file. The file pointer is set to the beginning of the file.
a+b or ab+ | Create a stream for reading and writing to a binary file in append mode. The file pointer is set to the end of the file.

Note: Use the w, w+, wb, wb+, and w+b modes with care; they can destroy existing files.

The specified type must be compatible with the access mode you used to open the file. If the file was opened with the O_APPEND FLAG, the stream mode must be r, a, a+, rb, ab, a+b, or ab+.

When you open a file with a, a+, ab, a+b, or ab+ as the value of type, all write operations take place at the end of the file. Although you can reposition the file pointer using fseek or rewind, the file pointer always moves back to the end of the file before the system carries out any write operation. This action prevents you from writing over existing data.

When you specify any of the types containing +, you can read from and write to the file, and the file is open for update. However, when switching from reading to writing or from writing to reading, you must include an intervening fseek, fsetpos, or rewind operation. You can specify the current file position with fseek.

In accessing text files, carriage-return line-feed (CR-LF) combinations are translated into a single line feed (LF) on input; LF characters are translated to CR-LF combinations on output. Accesses to binary files suppress all of these translations.

If fdopen returns NULL, use close to close the file. If fdopen is successful, you must use fclose to close the stream and file.

Note: In earlier releases of the C/C++ run-time library, fdopen began with an underscore (_fdopen). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _fdopen to fdopen for you.

Return Value
fdopen returns a pointer to a file structure that can be used to access the open file. A NULL pointer return value indicates an error.

Example

close -- Close File Associated with Handle
creat -- Create New File
fclose -- Close Stream
fopen -- Open Files
fseek -- Reposition File Position
fsetpos -- Set File Position
open -- Open File
rewind --Adjust Current File Position
_sopen -- Open Shared File
<stdio.h>