首页 > 代码库 > FATFS 初学之 f_truncate

FATFS 初学之 f_truncate

 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

函数功能:截断文件大小

描述:

f_truncate函数当 _FS_READONLY == 0 并且  _FS_MINIMIZE == 0时可用。
f_truncate函数截断文件到当前的文件读/写指针。当文件读/写指针已经指向文件结束时,该函数不起作用。