lseek -- Move File Pointer

Format

#include <io.h>  /* constants in <stdio.h> */
long lseek(int handle, long offset, int origin);

Language Level: XPG4, Extension
lseek moves any file pointer associated with handle to a new location that is offset bytes from the origin. The next operation on the file takes place at the new location. The origin must be one of the following constants, defined in <stdio.h>:

Origin Definition
SEEK_SET Beginning of file
SEEK_CUR Current position of file pointer
SEEK_END End of file

lseek can reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file; the data between the EOF and the new file position is undefined. An attempt to position the pointer before the beginning of the file causes an error.

Note: In earlier releases of the C/C++ run-time library, lseek began with an underscore (_lseek). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _lseek to lseek for you.

Return Value
lseek returns the offset, in bytes, of the new position from the beginning of the file. A return value of -1L indicates an error, and lseek sets errno to one of the following values:

Value Meaning
EBADF The file handle is incorrect.
EINVAL The value for origin is incorrect, or the position specified by offset is before the beginning of the file.
EOS2ERR The call to the operating system was not successful.

On devices incapable of seeking (such as keyboards and printers), lseek returns -1 and sets errno to EOS2ERR.

Example



_chsize -- Alter Length of File
fseek -- Reposition File Position
_tell -- Get Pointer Position
<io.h>
<stdio.h>