|
|
1.1 ! root 1: ! 2: ! 3: fdopen() STDIO Function fdopen() ! 4: ! 5: ! 6: ! 7: ! 8: Open a stream for standard I/O ! 9: ! 10: #iinncclluuddee <ssttddiioo.hh> ! 11: FFIILLEE *ffddooppeenn(_f_d, _t_y_p_e) iinntt _f_d; cchhaarr *_t_y_p_e; ! 12: ! 13: ffddooppeenn allocates and returns a FFIILLEE structure, or stream, for the ! 14: file descriptor fd, as obtained from open, creat, dduupp, or ppiippee. ! 15: type is the manner in which you want fd to be opened, as follows: ! 16: ! 17: ! 18: rr Read a file ! 19: ww Write into a file ! 20: aa Append onto a file ! 21: ! 22: ! 23: ***** Example ***** ! 24: ! 25: The following example obtains a file descriptor with open, and ! 26: then uses ffddooppeenn to build a pointer to the FFIILLEE structure. ! 27: ! 28: ! 29: #include <ctype.h> ! 30: #include <stdio.h> ! 31: ! 32: ! 33: ! 34: void adios(message) ! 35: char *message; ! 36: { ! 37: fprintf(stderr, "%s\n", message); ! 38: exit(1); ! 39: } ! 40: ! 41: ! 42: ! 43: main(argc, argv) ! 44: int argc; char *argv[]; ! 45: { ! 46: extern FILE *fdopen(); ! 47: FILE *fp; ! 48: int fd; ! 49: int holder; ! 50: ! 51: ! 52: ! 53: if (--argc != 1) ! 54: adios("Usage: example filename"); ! 55: ! 56: ! 57: ! 58: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: fdopen() STDIO Function fdopen() ! 70: ! 71: ! 72: ! 73: if ((fd = open(argv[1], 0)) == -1) ! 74: adios("open failed."); ! 75: if ((fp = fdopen(fd, "r")) == NULL) ! 76: adios("fdopen failed."); ! 77: ! 78: ! 79: ! 80: while ((holder = fgetc(fp)) != EOF) { ! 81: if ((holder > '\177') && (holder < ' ')) ! 82: switch(holder) { ! 83: case '\t': ! 84: case '\n': ! 85: break; ! 86: default: ! 87: fprintf(stderr, "Seeing char %d\n", holder); ! 88: exit(1); ! 89: } ! 90: fputc(holder, stdout); ! 91: } ! 92: } ! 93: ! 94: ! 95: ***** See Also ***** ! 96: ! 97: creat(), dup(), fopen(), open(), STDIO ! 98: ! 99: ***** Diagnostics ***** ! 100: ! 101: fdopen returns NULL if it cannot allocate a FFIILLEE structure. Cur- ! 102: rently, only 20 FFIILLEE structures can be allocated per program, in- ! 103: cluding ssttddiinn, ssttddoouutt, and ssttddeerrrr. ! 104: ! 105: ! 106: ! 107: ! 108: ! 109: ! 110: ! 111: ! 112: ! 113: ! 114: ! 115: ! 116: ! 117: ! 118: ! 119: ! 120: ! 121: ! 122: ! 123: ! 124: ! 125: ! 126: ! 127: ! 128: ! 129: ! 130: COHERENT Lexicon Page 2 ! 131: ! 132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.