![]() Chapter Contents |
![]() Previous |
![]() Next |
| fdopen |
| Portability: | POSIX.1 conforming, UNIX compatible |
| SYNOPSIS | |
| DESCRIPTION | |
| RETURN VALUE | |
| EXAMPLE | |
| RELATED FUNCTIONS | |
| SEE ALSO |
| SYNOPSIS |
#include <lcio.h> FILE *fdopen(int fileDescriptor, const char *options)
The synopsis for the POSIX implementation is
#include <stdio.h> FILE *fdopen(int fileDescriptor, const char *options)
| DESCRIPTION |
fdopen
associates an open USS file descriptor with a pointer to a FILE structure
enabling access to the file using standard I/O. This pointer enables you to
control buffering and to format input and output.
fdopen
accepts the
following
options
. These specified options
must not conflict with the current mode of the file descriptor:
| r or rb | Open for reading |
| w or wb | Open for writing |
| a or ab | Open for appending |
| r+ | Open for update |
| w+ | Open for update |
| a+ | Open for update at end of file |
If the options string includes a "b," the "b" is ignored.
The file position indicator of the new pointer is the file offset of the file
descriptor.
fdopen
clears the error indicator
and the end-of-file indicator for the pointer. When the file pointer is closed,
the file descriptor is also closed.
fdopen
can also permit
access to an open socket through a standard C FILE pointer.
| RETURN VALUE |
fdopen
returns a FILE pointer to the control block for the new pointer.
fdopen
returns NULL if not successful.
| EXAMPLE |
This example uses
fdopen
to open an HFS file for standard I/O, only if the file already
exists. This option is specified by the
O_EXCL
option bit for
open
, but there
is no equivalent
fopen
functionality.
/* This example must be compiled with the posix compiler option. */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
FILE *fopen_excl(char *pathname, char *openmode) {
int open_opts = O_TRUNC | O_CREAT | O_EXCL;
int fd;
FILE *fileptr;
/* Turn the fopen style openmode into open bits. */
/* We assume openmode is a valid open mode. */
if (openmode[0] == 'a') open_opts |= O_APPEND;
if (strchr(openmode, '+')) open_opts |= O_RDWR;
else open_opts |= O_WRONLY;
fd = open(pathname, open_opts, S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH);
if (fd < 0) { /* if the file wouldn't open */
perror("open error");
return NULL;
}
/* Make a FILE ptr for the fd. */
fileptr = fdopen(fd, openmode);
if (!fileptr) {
perror("fdopen error");
close(fd);
return NULL;
}
return fileptr;
}
| RELATED FUNCTIONS |
| SEE ALSO |
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.