|
|
1.1 ! root 1: ! 2: ! 3: ferror() STDIO Macro ferror() ! 4: ! 5: ! 6: ! 7: ! 8: Discover stream status ! 9: ! 10: #include <stdio.h> ! 11: iinntt ffeerrrroorr(_f_p) FFIILLEE *_f_p; ! 12: ! 13: ferror is a macro that tests the status of the file stream fp. ! 14: It returns a number other than zero if an error has occurred on ! 15: fp. Any error condition that is discovered will persist either ! 16: until the stream is closed or until clearerr is used to clear it. ! 17: For write routines that employ buffers, fflush should be called ! 18: before ferror, in case an error occurs on the last block written. ! 19: ! 20: ***** Example ***** ! 21: ! 22: This example reads a word from one file and writes it into ! 23: another. ! 24: ! 25: ! 26: #include <stdio.h> ! 27: ! 28: ! 29: ! 30: main() ! 31: { ! 32: FILE *fpin, *fpout; ! 33: int inerr = 0; ! 34: int outerr = 0; ! 35: int word; ! 36: char infile[20], outfile[20]; ! 37: ! 38: ! 39: ! 40: printf("Name data file you wish to copy:\n"); ! 41: gets(infile); ! 42: printf("Name new file:\n"); ! 43: gets(outfile); ! 44: ! 45: ! 46: ! 47: if ((fpin = fopen(infile, "r")) != NULL) { ! 48: if ((fpout = fopen(outfile, "w")) != NULL) { ! 49: ! 50: ! 51: ! 52: for (;;) { ! 53: word = fgetw(fpin); ! 54: if (ferror(fpin)) { ! 55: clearerr(fpin); ! 56: inerr++; ! 57: } ! 58: ! 59: ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: ferror() STDIO Macro ferror() ! 70: ! 71: ! 72: ! 73: if (feof(fpin)) ! 74: break; ! 75: fputw(word, fpout); ! 76: if (ferror(fpout)) { ! 77: clearerr(fpout); ! 78: outerr++; ! 79: } ! 80: } ! 81: ! 82: ! 83: ! 84: } else { ! 85: printf ! 86: ("Cannot open output file %s\n", ! 87: outfile); ! 88: exit(1); ! 89: } ! 90: ! 91: ! 92: ! 93: } else { ! 94: printf("Cannot open input file %s\n", infile); ! 95: exit(1); ! 96: } ! 97: ! 98: ! 99: ! 100: printf("%d - read error(s) %d - write error(s)\n", ! 101: inerr, outerr); ! 102: exit(0); ! 103: } ! 104: ! 105: ! 106: ***** See Also ***** ! 107: ! 108: STDIO ! 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.