1 /*-----------------------------------------------------------------------*/ 2 /* Truncate File */ 3 /*-----------------------------------------------------------------------*/ 4 5 FRESULT f_truncate ( 6 FIL *fp /* Pointer to the file object */ 7 ) 8 { 9 FRESULT res;10 DWORD ncl;11 12 13 res = validate(fp->fs, fp->id); /* Check validity of the object */14 if (res == FR_OK) {15 if (fp->flag & FA__ERROR) { /* Check abort flag */16 res = FR_INT_ERR;17 } else {18 if (!(fp->flag & FA_WRITE)) /* Check access mode */19 res = FR_DENIED;20 }21 }22 if (res == FR_OK) {23 if (fp->fsize > fp->fptr) {24 fp->fsize = fp->fptr; /* Set file size to current R/W point */25 fp->flag |= FA__WRITTEN;26 if (fp->fptr == 0) { /* When set file size to zero, remove entire cluster chain */27 res = remove_chain(fp->fs, fp->sclust);28 fp->sclust = 0;29 } else { /* When truncate a part of the file, remove remaining clusters */30 ncl = get_fat(fp->fs, fp->clust);31 res = FR_OK;32 if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;33 if (ncl == 1) res = FR_INT_ERR;34 if (res == FR_OK && ncl < fp->fs->n_fatent) {35 res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);36 if (res == FR_OK) res = remove_chain(fp->fs, ncl);37 }38 }39 }40 if (res != FR_OK) fp->flag |= FA__ERROR;41 }42 43 LEAVE_FF(fp->fs, res);44 }View code
Function: truncates the file size.
Description:
The f_truncate function is available when _ fs_readonly = 0 and _ fs_minimize = 0.
The f_truncate function truncates the file to the current file read/write pointer. This function does not work when the file read/write Pointer Points to the end of the file.