The f_lseek function moves the file read/write pointer of an open file object. It can also be used to increase the file size (cluster pre-allocation).
FRESULT f_lseek ( FIL* FileObject, /* Pointer to the file object structure */ DWORD Offset /* File offset in unit of byte */ );
The f_lseek function moves the file read/write pointer of an open file. The offset can be specified in only origin from top of the file. When an offset above the file size is specified in write mode, the file size is increased and the data in the expanded area is undefined. This is suitable to create a large file quickly, for fast write operation. After the f_lseek function succeeded, member fptr in the file object should be checked in order to make sure the read/write pointer has been moved correctry. In case of fptr is not the expected value, either of followings has been occured.
When _USE_FASTSEEK is set to 1 and cltbl member in the file object is not NULL, the fast seek feature is enabled. This feature enables fast backward/long seek operations without FAT access by cluster link information stored on the user defined table. The cluster link information must be created prior to do the fast seek. The required size of the table is (number of fragments + 1) * 2 items. For example, when the file is fragmented in 5, 12 items will be required to store the cluster link information. The file size cannot be expanded when the fast seek feature is enabled.
Available when _FS_MINIMIZE <= 2.
/* Move to offset of 5000 from top of the file */ res = f_lseek(file, 5000); /* Move to end of the file to append data */ res = f_lseek(file, file->fsize); /* Forward 3000 bytes */ res = f_lseek(file, file->fptr + 3000); /* Rewind 2000 bytes (take care on overflow) */ res = f_lseek(file, file->fptr - 2000);
/* Cluster pre-allocation (to prevent buffer overrun on streaming write) */ res = f_open(file, recfile, FA_CREATE_NEW | FA_WRITE); /* Create a file */ res = f_lseek(file, PRE_SIZE); /* Pre-allocate clusters */ if (res || file->fptr != PRE_SIZE) ... /* Check if the file size has been increased correctly */ res = f_lseek(file, DATA_START); /* Record data stream without cluster allocation delay */ ... res = f_truncate(file); /* Truncate unused area */ res = f_lseek(file, 0); /* Put file header */ ... res = f_close(file);
/* Using fast seek feature */ DWORD lktbl[SZ_TBL]; /* Link map table buffer */ res = f_lseek(&file, ofs1); /* This is normal seek (file.cltbl == NULL on file open) */ file.cltbl = lktbl; /* Enable fast seek feature */ lktbl[0] = SZ_TBL; /* Set table size to the first item */ res = f_lseek(&file, CREATE_LINKMAP); /* Create cluster link map table */ ... res = f_lseek(&file, ofs2); /* This is fast seek (file.cltbl != NULL) */