_lseek, _lseeki64
Move a file pointer to the specified location.
long _lseek( int fd, long offset, int origin ); __int64 _lseeki64( int fd, __int64 offset, int origin );
Parameters
- fd
- File descriptor referring to open file.
- offset
- Number of bytes from origin.
- origin
- Initial position.
Return Value
_lseek returns the offset, in bytes, of the new position from the beginning of the file. _lseeki64 returns the offset in a 64-bit integer. The function returns –1L to indicate an error and sets errno either to EBADF, meaning the file descriptor is invalid, or to EINVAL, meaning the value for origin is invalid or the position specified by offset is before the beginning of the file. On devices incapable of seeking (such as terminals and printers), the return value is undefined.
See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, return codes.
Remarks
The _lseek function moves the file pointer associated with fd to a new location that is offset bytes from origin. The next operation on the file occurs at the new location. The origin argument must be one of the following constants, which are defined in STDIO.H:
- SEEK_SET
- Beginning of file.
- SEEK_CUR
- Current position of file pointer.
- SEEK_END
- End of file.
You can use _lseek to reposition the pointer anywhere in a file or beyond the end of the file.
Requirements
| Routine | Required header | Compatibility |
|---|---|---|
| _lseek | <io.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
| _lseeki64 | <io.h> | Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_lseek.c
/* This program first opens a file named lseek.txt.
* It then uses _lseek to find the beginning of the file,
* to find the current position in the file, and to find
* the end of the file.
*/
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
int fh;
long pos; /* Position of file pointer */
char buffer[10];
fh = _open( "crt_lseek.txt", _O_RDONLY );
/* Seek the beginning of the file: */
pos = _lseek( fh, 0L, SEEK_SET );
if( pos == -1L )
perror( "_lseek to beginning failed" );
else
printf( "Position for beginning of file seek = %ld\n", pos );
/* Move file pointer a little */
_read( fh, buffer, 10 );
/* Find current position: */
pos = _lseek( fh, 0L, SEEK_CUR );
if( pos == -1L )
perror( "_lseek to current position failed" );
else
printf( "Position for current position seek = %ld\n", pos );
/* Set the end of the file: */
pos = _lseek( fh, 0L, SEEK_END );
if( pos == -1L )
perror( "_lseek to end failed" );
else
printf( "Position for end of file seek = %ld\n", pos );
_close( fh );
}
Input: crt_lseek.txt
Line one. Line two. Line three. Line four. Line five.
Output
Position for beginning of file seek = 0 Position for current position seek = 10 Position for end of file seek = 57
See Also
Low-Level I/O Routines | fseek | _tell | Run-Time Routines and .NET Framework Equivalents