

fdopen()                  STDIO Function                 fdopen()




Open a stream for standard I/O

#iinncclluuddee <ssttddiioo.hh>
FFIILLEE *ffddooppeenn(_f_d, _t_y_p_e) iinntt _f_d; cchhaarr *_t_y_p_e;

ffddooppeenn allocates and returns a FFIILLEE structure, or stream, for the
file descriptor  fd, as obtained from open,  creat, dduupp, or ppiippee.
type is the manner in which you want fd to be opened, as follows:


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


***** Example *****

The following  example obtains a  file descriptor with  open, and
then uses ffddooppeenn to build a pointer to the FFIILLEE 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");









COHERENT Lexicon                                           Page 1




fdopen()                  STDIO Function                 fdopen()



        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(), open(), STDIO

***** Diagnostics *****

fdopen returns NULL if it cannot allocate a FFIILLEE structure.  Cur-
rently, only 20 FFIILLEE structures can be allocated per program, in-
cluding ssttddiinn, ssttddoouutt, and ssttddeerrrr.


























COHERENT Lexicon                                           Page 2


