COHERENT manpages

This page displays the COHERENT manpage for fdopen() [Open a stream for standard I/O].

List of available manpages
Index


fdopen() -- STDIO Function (libc)

Open a stream for standard I/O
#include <stdio.h>
FILE *fdopen(fd, type) int fd; char *type;

fdopen() allocates  and returns a  FILE structure, or stream,  for the file
descriptor fd, as obtained from open(), creat(), dup(), or pipe().  type is
the manner in which you want fd to be opened, as follows:

    r   Read a file
    w   Write into a file
    a   Append onto a file

Example

The following example obtains a  file descriptor with open(), and then uses
fdopen() to build a pointer to the FILE structure.

#include <ctype.h>
#include <stdio.h>

void adios(message)
char *message;
{
    fprintf(stderr, "%s\n", message);
    exit(1);
}

main(argc, argv)
int argc; char *argv[];
{
    extern FILE *fdopen();
    FILE *fp;
    int fd;
    int holder;

    if (--argc != 1)
        adios("Usage: example filename");

    if ((fd = open(argv[1], 0)) == -1)
        adios("open failed.");
    if ((fp = fdopen(fd, "r")) == NULL)
        adios("fdopen failed.");

    while ((holder = fgetc(fp)) != EOF) {
        if ((holder > '\177') || (holder < ' '))
            switch(holder) {
            case '\t':
            case '\n':
                break;
            default:
                fprintf(stderr, "Seeing char %d\n", holder);
                exit(1);
            }
        fputc(holder, stdout);
    }
}

See Also

creat(),
dup(),
fopen(),
libc,
open()
POSIX Standard, §8.2.2

Diagnostics

fdopen() returns  NULL if it cannot allocate  a FILE structure.  Currently,
only  20 FILE  structures can  be allocated  per program,  including stdin,
stdout, and stderr.