This documentation is archived and is not being maintained.

_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 


File descriptor referring to open file.
Number of bytes from 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.


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:

Beginning of file.
Current position of file pointer.
End of file.

You can use _lseek to reposition the pointer anywhere in a file or beyond the end of the file.


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.


All versions of the C run-time libraries.


// 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" );
      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" );
      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" );
      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.


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