app1.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. }