app1.c 1020 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*------------------------------------------------------------/
  2. / Open or create a file in append mode
  3. /------------------------------------------------------------*/
  4. FRESULT open_append (
  5. FIL* fp, /* [OUT] File object to create */
  6. const char* path /* [IN] File name to be opened */
  7. )
  8. {
  9. FRESULT fr;
  10. /* Opens an existing file. If not exist, creates a new file. */
  11. fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
  12. if (fr == FR_OK) {
  13. /* Seek to end of the file to append data */
  14. fr = f_lseek(fp, f_size(fp));
  15. if (fr != FR_OK)
  16. f_close(fp);
  17. }
  18. return fr;
  19. }
  20. int main (void)
  21. {
  22. FRESULT fr;
  23. FATFS fs;
  24. FIL fil;
  25. /* Open or create a log file and ready to append */
  26. f_mount(&fs, "", 0);
  27. fr = open_append(&fil, "logfile.txt");
  28. if (fr != FR_OK) return 1;
  29. /* Append a line */
  30. f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);
  31. /* Close the file */
  32. f_close(&fil);
  33. return 0;
  34. }