Function name : lseek
Header file : <io.h>
Function prototype : int lseek(int handle,long offset,long length);
Function : Used to move the pointer of the open file
Parameters : int handle is the file handle to which the file pointer is to be moved.
long offset is the offset to be moved
int fromwhere is the direction in which the offset of the file pointer is calculated.
There are three values:
SEEK_SET Beginning of file
SEEK_CUR Current location of the file
SEEK_END end of file
Return value : The position of the file pointer after moving the file pointer
Program example : Create a file with the content I like www.dotcpp.com very much!
//Open the file, skip 7 bytes, and take 14 characters. #include<stdio.h>#include<io.h>#include<fcntl.h>intmain(void){intfd=open(D:\a.txt,O_RDONLY);if(fd==-1){ printf(cannotopenthefilen);return1;}intpos=tell(fd);printf(beforelseekfunction,currentposition:%ldn,pos);lseek(fd,7,SEEK_SET);//Move to the beginning offset of the file 7-byte position charbuf[20]={n};read(fd,buf,14);printf(theresis%sn,buf);pos=tell(fd);printf(afterlseekfunction,currentposition:% ldn,pos);close(fd);return0;}
Running results
beforelseekfunction,currentposition:0theresiswww.dotcpp.comafterlseekfunction,currentposition:21