]> cloudbase.mooo.com Git - z180-stamp.git/blob - fatfs/doc/img/app1.c
Import fatfs R0.10c
[z180-stamp.git] / fatfs / doc / img / 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