Definition | #include <stdio.h> off__t lseek(int fd, off_t offset, int loc);
To process files > 2 GB, proceed as follows:
There is no functional difference between Text files (SAM, ISAM) can be absolutely positioned to the beginning or end of the file as well as to any position previously marked with tell. Binary files (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). SAM files are always processed as text files with elementary text functions. SAM files are processed as text files with elementary functions except when O_RECORD was specified for | |||||||||||||||||||||||||||||||||||||||||||||||
Parameters | int fd File descriptor of the file whose read/write pointer is to be positioned. off_t / off64_t offset, int loc Since the meaning, combination options, and effects of these parameters differ for text and binary files, they are individually described in the following. Text files (SAM, ISAM)Possible parameter values:
Meaningful combinations and their effects
Binary files(PAM, INCORE)Possible parameter values:
Meaningful combinations and their effects
| |||||||||||||||||||||||||||||||||||||||||||||||
Return val. | The position in the file if successful, i.e. | |||||||||||||||||||||||||||||||||||||||||||||||
for binary files, the number of bytes that offsets the read/write pointer from the beginning of the file; | ||||||||||||||||||||||||||||||||||||||||||||||||
-1 | if an error occurs. In addition, the corresponding error information is stored in the | |||||||||||||||||||||||||||||||||||||||||||||||
Notes | The If new records are written to a text file (opened for creation or in append mode) and an Exception for ANSI functionality: If you position past the end of file in the case of a 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. System files (SYSDTA, SYSLST, SYSOUT) cannot be positioned. Since information on the file position is combined in a single field, the following restrictions apply to the size of SAM and ISAM files: SAM file
ISAM file
| |||||||||||||||||||||||||||||||||||||||||||||||
Record I/O |
| |||||||||||||||||||||||||||||||||||||||||||||||
Example | The following program reads the file passed as the first argument in the call from position 10 onwards and appends its contents to the end of another file if a second argument is specified. Otherwise, it writes to the standard output (only works with binary files, i.e. with PAM files in this case): #include <stdio.h> #include <stdlib> int fd1, fd2; long result; char c; int main(int argc, char *argv[]) { if((fd1 = open (argv[1],0)) < 0) exit(1); if(argc < 3) fd2 = 1; else fd2 = open(argv[2], 1); result = lseek(fd1, 10L, SEEK_SET); printf("current position in file1 : %ld\n", tell(fd1)); /* Other possible position queries: printf("current position in file1: %ld\n, result); printf("current position in file1: %ld\n, lseek(fd1, 0L, SEEK_CUR)); */ while(read(fd1, &c, 1) > 0) write(fd2, &c, 1); close(fd1); close(fd2); } | |||||||||||||||||||||||||||||||||||||||||||||||
See also | tell, fseek, fseek64, ftell, ftell64 |