|
|
1.1 ! root 1: ! 2: ! 3: fseek() STDIO Function fseek() ! 4: ! 5: ! 6: ! 7: ! 8: Seek on file stream ! 9: ! 10: #include <stdio.h> ! 11: iinntt ffsseeeekk(_f_p, _w_h_e_r_e, _h_o_w) ! 12: FFIILLEE *_f_p; lloonngg _w_h_e_r_e; iinntt _h_o_w; ! 13: ! 14: fseek changes where the next read or write operation will occur ! 15: within the file stream fp. It handles any effects the seek ! 16: routine might have had on the internal buffering strategies of ! 17: the system. The arguments where and how specify the desired seek ! 18: position. where indicates the new seek position in the file. It ! 19: is measured from the start of the file if how equals zero, from ! 20: the current seek position if how equals one, and from the end of ! 21: the file if how equals two. ! 22: ! 23: fseek differs from its cousin lseek in that lseek is a COHERENT ! 24: system call and takes a file number, whereas fseek is a STDIO ! 25: function and takes a FILE pointer. ! 26: ! 27: ***** Example ***** ! 28: ! 29: This example opens file argv[1] and prints its last argv[2] ! 30: characters (default, 100). It demonstrates the functions fseek, ! 31: ftell, and fclose. ! 32: ! 33: ! 34: #include <stdio.h> ! 35: extern long atol(); ! 36: ! 37: ! 38: ! 39: void fatal(message) ! 40: char *message; ! 41: { ! 42: fprintf(stderr, "tail: %s\n", s); ! 43: exit(1); ! 44: } ! 45: ! 46: ! 47: ! 48: main(argc, argv) ! 49: int argc; char *argv[]; ! 50: { ! 51: register FILE *ifp; ! 52: register int c; ! 53: long nchars, size; ! 54: ! 55: ! 56: ! 57: if (argc < 2 || argc > 3) ! 58: fatal("Usage: tail file [ nchars ]"); ! 59: nchars = (argc == 3) ? atol(argv[2]) : 100L; ! 60: ! 61: ! 62: ! 63: ! 64: COHERENT Lexicon Page 1 ! 65: ! 66: ! 67: ! 68: ! 69: fseek() STDIO Function fseek() ! 70: ! 71: ! 72: ! 73: ! 74: ! 75: if ((ifp = fopen(argv[1], "r")) == NULL) ! 76: fatal("cannot open input file"); ! 77: /* Seek to end */ ! 78: if (fseek(ifp, 0L, 2) == -1) ! 79: fatal("seek error"); ! 80: ! 81: ! 82: ! 83: /* Find current size */ ! 84: size = ftell(ifp); ! 85: size = (size < nchars) ? 0L : size - nchars; ! 86: ! 87: ! 88: ! 89: /* Seek to point */ ! 90: if (fseek(ifp, size, 0) == -1) ! 91: fatal("seek error"); ! 92: while ((c = getc(ifp)) != EOF) ! 93: /* Copy rest to stdout */ ! 94: putchar(c); ! 95: if (fclose(ifp) == EOF) ! 96: fatal("cannot close"); ! 97: exit(0); ! 98: } ! 99: ! 100: ! 101: ***** See Also ***** ! 102: ! 103: ftell(), lseek(), STDIO ! 104: ! 105: ***** Diagnostics ***** ! 106: ! 107: For any diagnostic error, fseek returns -1; otherwise, it returns ! 108: zero. If fseek goes beyond the end of the file, it will not ! 109: return an error message until the corresponding read or write is ! 110: performed. ! 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.