_fdopen

Description

Associates a stream with a file that was previously opened for low-level I/O.

#include <stdio.h>

FILE *_fdopen( inthandle, char *mode );

handle Handle referring to open file  
mode Type of access permitted  

Remarks

The _fdopen function associates an input/output stream with the file identified by handle, thus allowing a file opened for low-level I/O to be buffered and formatted. (For an explanation of stream I/O and low-level I/O see “Input and Output”.) The mode character string specifies the type of access requested for the file, as shown below. The following list gives the mode string used in the fopen and _fdopen functions and the corresponding oflag arguments used in the _open and _sopen functions. A complete description of the mode string argument is given in the remarks section of the fopen function.

Type String Equivalent Value for _open/_sopen

"r" _O_RDONLY
"w" _O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC)
"a" _O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)
"r+" _O_RDWR
"w+" _O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)
"a+" _O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )

In addition to the values listed above, one of the following characters can be included in the mode string to specify the translation mode for new lines. These characters correspond to the constants used in the _open and _sopen functions, as shown below:

Mode Equivalent Value for _open/_sopen

t _O_TEXT
b _O_BINARY

If t or b is not given in the mode string, the translation mode is defined by the default-mode variable _fmode.

In addition to the file attribute and the text or binary mode listed above, the mode string accepts either c or n to specify commit to disk, or do not commit to disk, respectively. These characters have no correspondence to constants used in the _open and _sopen functions. For more information on the commit feature, see “Committing Buffer Contents to Disk”37.

Mode Description

c Commit to disk, no _open/_sopen equivalent.
n No commit, no _open/_sopen equivalent. Default.

If c or n is not given in the mode string, n is the default mode.

Return Value

The _fdopen function returns a pointer to the open stream. A null pointer value indicates an error.

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _fdopen for compatibility with ANSI naming conventions of non-ANSI functions. Use fdopen and link with OLDNAMES.LIB for UNIX compatibility.

The t, c, and n options are not part of the ANSI standard for fopen and _fdopen, but are instead Microsoft extensions and should not be used where ANSI portability is desired.

See Also

_dup, _dup2, fclose, _fcloseall, fopen, freopen, _open

Example

/* _FDOPEN.C: This program opens a file using low-level I/O, then uses

* _fdopen to switch to stream access. It counts the lines in the file.

*/

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <io.h>

void main( void )

{

FILE *stream;

int fh, count = 0;

char inbuf[128];

/* Open a file handle. */

if( (fh = _open( "_fdopen.c", _O_RDONLY )) == -1 )

exit( 1 );

/* Change handle access to stream access. */

if( (stream = _fdopen( fh, "r" )) == NULL )

exit( 1 );

while( fgets( inbuf, 128, stream ) != NULL )

count++;

/* After _fdopen, close with fclose, not _close. */

fclose( stream );

printf( "Lines in file: %d\n", count );

}

Output

Lines in file: 31