CARME-M4 BSP  V1.5
app1.c
1 /*------------------------------------------------------------/
2 / Open or create a file in append mode
3 /------------------------------------------------------------*/
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 
12  /* Opens an existing file. If not exist, creates a new file. */
13  fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
14  if (fr == FR_OK) {
15  /* Seek to end of the file to append data */
16  fr = f_lseek(fp, f_size(fp));
17  if (fr != FR_OK)
18  f_close(fp);
19  }
20  return fr;
21 }
22 
23 
24 int main (void)
25 {
26  FRESULT fr;
27  FATFS fs;
28  FIL fil;
29 
30  /* Open or create a log file and ready to append */
31  f_mount(&fs, "", 0);
32  fr = open_append(&fil, "logfile.txt");
33  if (fr != FR_OK) return 1;
34 
35  /* Append a line */
36  f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);
37 
38  /* Close the file */
39  f_close(&fil);
40 
41  return 0;
42 }
43 
FRESULT f_lseek(FIL *fp, DWORD ofs)
Seek File R/W Pointer.
Definition: ff.c:3005
FRESULT
File function return code.
Definition: ff.h:204
File object structure.
Definition: ff.h:127
#define FA_WRITE
Definition: ff.h:385
File system object structure.
Definition: ff.h:88
#define FA_OPEN_ALWAYS
Definition: ff.h:402
#define f_size(fp)
Definition: ff.h:323
FRESULT f_close(FIL *fp)
Close File.
Definition: ff.c:2822
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Mount/Unmount a Logical Drive.
Definition: ff.c:2392
Definition: ff.h:205
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Open or Create a File.
Definition: ff.c:2434