Definition | #include <stdio.h> int fseek(FILE *fp, long offset, int loc);
Text files (SAM in text mode, ISAM) can be positioned absolutely to the beginning or end of the file as well as to any position previously marked with Binary files (SAM in binary mode, PAM, INCORE) can be positioned absolutely (see above) or relatively, i.e. relative to beginning of file, end of file, or current position (by a desired number of bytes). To process files > 2 GB, proceed as follows:
There is no functional difference between | |||||||||||||||||||||||||||||||||||||||||||||||
Parameters | FILE *fp File pointer for the file whose read/write pointer is to be positioned. long offset / off_t offset / long long offset / off64_t offset Since the meaning, combination options, and effect of these parameters differ for text and binary files, they are individually described in the following: Text files (SAM in text mode, ISAM) Possible parameter values:
Meaningful combinations and their effects:
Binary files (SAM in binary mode, PAM, INCORE) Possible parameter values:
Meaningful combinations and their effects:
| |||||||||||||||||||||||||||||||||||||||||||||||
Return val. | 0 | if successful. | ||||||||||||||||||||||||||||||||||||||||||||||
-1 | if an error occurred. | |||||||||||||||||||||||||||||||||||||||||||||||
Hinweise | If new records are written to a text file that was opened in the write or append mode and an If you position past the end of binary file opened for writing, a “gap” appears between the last physically stored data and the newly written data. Reading from this “gap” returns binary zeros. It is not possible to position to system files (SYSDTA, SYSLST, SYSOUT). A successful | |||||||||||||||||||||||||||||||||||||||||||||||
Record I/O |
If called with any other arguments, | |||||||||||||||||||||||||||||||||||||||||||||||
Example 1 | The following program reads file from the eleventh character to the end of the file (only functions for binary files). #include <stdio.h> int main(void) { FILE *fp; int c; if((fp = fopen("file","rb")) != NULL) { /* skip the first 10 characters */ fseek(fp,10L,SEEK_SET); while((c=getc(fp)) != EOF) putc((char)c,stdout); fclose(fp); } return 0; } | |||||||||||||||||||||||||||||||||||||||||||||||
Example 2 | The following program processes a file in the update mode. Lowercase letters are written back as uppercase letters; all other characters remain unchanged. #include <stdio.h> #include <ctype.h> int main(void) { FILE *fp; int c; long n; fp = fopen("link=link","r+w"); do { n = ftell(fp); c = getc(fp); if (islower(c) == 0) continue; /* If character is not in lowercase, */ /* read next character */ else { /* If character is in lowercase, */ fseek(fp, n, SEEK_SET); /* position to this character and */ fputc((toupper(c)), fp); /* write it back in uppercase. */ } } while(c != EOF); fclose(fp); return 0; } | |||||||||||||||||||||||||||||||||||||||||||||||
See also | ftell, ftello, ftell64, ftello64, fsetpos, fsetpos64, lseek, lseek64, rewind, tell |