0
\$\begingroup\$

The code is to log data in multiple files on SD card. The problem is that the SD card only opens and saves 1 file. When the if condition occurs in the 2nd time, the old file is deleted and the SD card is empty.

The condition here is that if a is between 110000 && 95000, time should be logged into the SD Card.

code:

char hours[5];
char seconds[2];
char min[2];
char year[4];
char month[2];
char day[2];

sprintf(hours, "%d" , sTime.Hours);
sprintf(day, "%d", sDate.WeekDay);
sprintf(seconds, "%d", sTime.Seconds);
sprintf(min, "%d", sTime.Minutes);
sprintf(year, "%d", sDate.Year);
sprintf(month, "%d", sDate.Month);

   /* Mount SD Card */
   if(f_mount(&fs, "", 0) != FR_OK)
      Error_Handler();  

   /* Open file to write */
   if(a <=110000 && a>=95000) {
      if(f_open(&fil,"second.txt", FA_CREATE_NEW | FA_READ | FA_WRITE) != FR_OK)
         Error_Handler();  
   }

   /* Check free space */
   if(f_getfree("", &fre_clust, &pfs) != FR_OK)
      Error_Handler();  

   total = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
   free1 = (uint32_t)(fre_clust * pfs->csize * 0.5);   

   /* Free space is less than 1kb */
   if(free1 < 1)
      Error_Handler();  

   /* Writing text */
   f_puts(hours, &fil);  
   f_puts(min, &fil);
   f_puts(seconds, &fil);
   f_puts("\n", &fil);
   f_puts(year, &fil);
   f_puts(month, &fil);
   f_puts(day, &fil);

   /* Close file */
   if(f_close(&fil) != FR_OK)
      Error_Handler();  

   /* Open file to read */
   if(f_open(&fil, "second.txt", FA_READ) != FR_OK)
      Error_Handler();  

   while(f_gets(buffer, sizeof(buffer), &fil))
   {
     //printf("%s", buffer);
   }

  /* Close file */
   if(f_close(&fil) != FR_OK)
      Error_Handler();  

   /* Unmount SDCARD */
   if(f_mount(NULL, "", 1) != FR_OK)
      Error_Handler();
\$\endgroup\$
5
  • 2
    \$\begingroup\$ so, why don't you use a different file name? ... it is unclear what is the actual problem that you are trying to solve \$\endgroup\$ – jsotola May 25 '20 at 0:10
  • 1
    \$\begingroup\$ It's not clear what your expectations are here. You only ever open a file called second.txt, so that's the only file you'll see. You're also opening it with the FA_CREATE_NEW flag, so every time you run this code, it'll be re-created as an empty file - because that's what you asked for. \$\endgroup\$ – brhans May 25 '20 at 1:38
  • \$\begingroup\$ its like sensor data logger, whenever a is between 1100000 & 95000, the time chould be saved on the sd card , in my code , the first time a is betwwen the two numbers, the time saved without any problem, but when a is between these two numbers in the second time, the file on sd destroyed, so how can i save multiple files not only one, each file opens when a is between the numbers i mentioned before. \$\endgroup\$ – Kilwa May 25 '20 at 10:31
  • \$\begingroup\$ You're just repeating yourself - I'm not asking what you hope for - that's already spelled out in your question - I'm asking how you expect to get multiple files when the only file you ever open and write to is called "second.txt". If you want multiple files then you would surely need to open different files ... ? \$\endgroup\$ – brhans May 25 '20 at 14:14
  • \$\begingroup\$ and thats what i need, i dont know how to open different files< and iam asking about that... \$\endgroup\$ – Kilwa May 28 '20 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.