硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2070|回复: 0
收起左侧

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

[复制链接]

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106718
QQ
发表于 2019-2-20 01:28:59 | 显示全部楼层 |阅读模式
http://elm-chan.org/fsw/ff/res/app1.c


  1. /*------------------------------------------------------------/
  2. / Open or create a file in append mode
  3. / (This function was sperseded by FA_OPEN_APPEND flag at FatFs R0.12a)
  4. /------------------------------------------------------------*/

  5. FRESULT open_append (
  6.     FIL* fp,            /* [OUT] File object to create */
  7.     const char* path    /* [IN]  File name to be opened */
  8. )
  9. {
  10.     FRESULT fr;

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


  21. int main (void)
  22. {
  23.     FRESULT fr;
  24.     FATFS fs;
  25.     FIL fil;

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

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

  32.     /* Close the file */
  33.     f_close(&fil);

  34.     return 0;
  35. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-4-30 19:28 , Processed in 0.243661 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表