eric2013 发表于 2019-2-20 01:28:59

FatFS文件末尾附加数据的方法

http://elm-chan.org/fsw/ff/res/app1.c

static/image/hrline/4.gif
/*------------------------------------------------------------/
/ Open or create a file in append mode
/ (This function was sperseded by FA_OPEN_APPEND flag at FatFs R0.12a)
/------------------------------------------------------------*/

FRESULT open_append (
    FIL* fp,            /* File object to create */
    const char* path    /* File name to be opened */
)
{
    FRESULT fr;

    /* Opens an existing file. If not exist, creates a new file. */
    fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
    if (fr == FR_OK) {
      /* Seek to end of the file to append data */
      fr = f_lseek(fp, f_size(fp));
      if (fr != FR_OK)
            f_close(fp);
    }
    return fr;
}


int main (void)
{
    FRESULT fr;
    FATFS fs;
    FIL fil;

    /* Open or create a log file and ready to append */
    f_mount(&fs, "", 0);
    fr = open_append(&fil, "logfile.txt");
    if (fr != FR_OK) return 1;

    /* Append a line */
    f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);

    /* Close the file */
    f_close(&fil);

    return 0;
}


页: [1]
查看完整版本: FatFS文件末尾附加数据的方法