lseek

Sets the current file position

Local Servers:nonblocking
Remote Servers:nonblocking
Classification:POSIX
Platform:NLM
Service:Operating System I/O

Syntax

  #include <unistd.h>  
   
  LONG lseek  (  
     int    handle,   
     LONG   offset,   
     int    origin);
  

Parameters

handle
(IN) Specifies a file handle.
offset
(IN) Specifies the relative offset from a file position.
origin
(IN) Specifies the seek starting point.

Return Values

On success, lseek returns the new current file position in a system-dependent manner. A value of 0 indicates the start of a file. If an error occurs, lseek returns 0xFFFFFFFF, and errno is set to:.

4

EBADF

Bad file number.

If lseek does not complete successfully, NetWareErrno is also set.

Remarks

The file is referenced using the specified file handle.

The value of the offset parameter is used as a relative offset from a file position determined by the value of the origin parameter. An absolute offset can be from 0 to 2 32. It must be unsigned long, which cannot be negative.

The new file position is determined in a manner dependent upon the value of the origin parameter, which can have one of three possible values (defined in the STDIO.H header file):

SEEK_SET

The new file position is computed relative to the start of the file.

SEEK_CUR

The new file position is computed relative to the current file position.

SEEK_END

The new file position is computed relative to the end of the file.

The files’s position can be set to a position outside of the bounds of the file.

See Also

close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, open, read, sopen, tell, write

Example

  #include <stddef.h>  
  #include <stdio.h>  
  #include <stdlib.h>  
  #include <unistd.h>  
  #include <fcntl.h>  
  #include <errno.h>  
   
  main()  
  {  
     int   fh;  
     if((fh = open ("test.dat",O_RDWR | O_CREAT | O_TRUNC,0)) < 0)  
     {  
        printf ("could not open file\r\n");  
        exit(0);  
     }  
     write (fh,"1234567890",10);  
     if(lseek (fh,1,SEEK_SET) < 0)  
     {  
        printf ("error on seek 1\r\n");  
        goto end;  
     }  
     write (fh,"a",1);  
     if(lseek(fh,-2,SEEK_END) < 0)  
     {  
        printf ("error on seek 2\r\n");  
        goto end;  
     }  
     write (fh,"b",1);  
     if(lseek (fh,-5,SEEK_CUR) < 0)  
     {  
        printf ("error on seek 3\r\n");  
        goto end;  
     }  
     write (fh,"c",1);  
     end:  
     close (fh);  
     getch ();  
  }