F_truncate for beginners of fatfs

Source: Internet
Author: User
 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.