xref: /third_party/FatFs/documents/res/app1.c (revision e6865dcd)
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
6FRESULT open_append (
7    FIL* fp,            /* [OUT] File object to create */
8    const char* path    /* [IN]  File name to be opened */
9)
10{
11    FRESULT fr;
12
13    /* Opens an existing file. If not exist, creates a new file. */
14    fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
15    if (fr == FR_OK) {
16        /* Seek to end of the file to append data */
17        fr = f_lseek(fp, f_size(fp));
18        if (fr != FR_OK)
19            f_close(fp);
20    }
21    return fr;
22}
23
24
25int main (void)
26{
27    FRESULT fr;
28    FATFS fs;
29    FIL fil;
30
31    /* Open or create a log file and ready to append */
32    f_mount(&fs, "", 0);
33    fr = open_append(&fil, "logfile.txt");
34    if (fr != FR_OK) return 1;
35
36    /* Append a line */
37    f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);
38
39    /* Close the file */
40    f_close(&fil);
41
42    return 0;
43}
44
45