lseek
Sets the current file position
#include <unistd.h>
LONG lseek (
int handle,
LONG offset,
int origin);
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:.
If lseek does not complete successfully, NetWareErrno is also set.
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):
The files’s position can be set to a position outside of the bounds of the file.
close, creat, dup, dup2, eof, filelength, fileno, fstat, isatty, open, read, sopen, tell, write
#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 ();
}