fdopen() STDIO Function fdopen()
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");
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 FILE structure. Cur-
rently, only 20 FILE structures can be allocated per program, in-
cluding stdin, stdout, and stderr.
COHERENT Lexicon Page 2