SAMV71 Xplained Ultra Software Package 1.0

app1.c

00001 /*------------------------------------------------------------/
00002 / Open or create a file in append mode
00003 /------------------------------------------------------------*/
00004 
00005 FRESULT open_append (
00006     FIL* fp,            /* [OUT] File object to create */
00007     const char* path    /* [IN]  File name to be opened */
00008 )
00009 {
00010     FRESULT fr;
00011 
00012     /* Opens an existing file. If not exist, creates a new file. */
00013     fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS);
00014     if (fr == FR_OK) {
00015         /* Seek to end of the file to append data */
00016         fr = f_lseek(fp, f_size(fp));
00017         if (fr != FR_OK)
00018             f_close(fp);
00019     }
00020     return fr;
00021 }
00022 
00023 
00024 int main (void)
00025 {
00026     FRESULT fr;
00027     FATFS fs;
00028     FIL fil;
00029 
00030     /* Open or create a log file and ready to append */
00031     f_mount(&fs, "", 0);
00032     fr = open_append(&fil, "logfile.txt");
00033     if (fr != FR_OK) return 1;
00034 
00035     /* Append a line */
00036     f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min);
00037 
00038     /* Close the file */
00039     f_close(&fil);
00040 
00041     return 0;
00042 }
00043 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines