SdFat: ename file with variable filename on a SD card does not work properly

Hello to everybody,
i tried now since 4 hours to get the problem solved to rename a file on the SD card with a variable filename using the SdFat library.
My task:
i am reading a lot of sensors and am stroing these values in a file called DL_SC.CSV on the SD card. this works fine.
Now every hour i want to rename that file into a new file, which I want to mail to an email account. In order to distinguish all the files the file, whic shall be sent, shall have the filename of the current month-day-hour-second.
the program produces out of the timestamp a name in the format mmddhhss.csv as a char array. This works also fine.
But when it comes now to rename the old file “DL_SC.CSV” to that new filename, e.g. “10141830.csv” it does very crazy things.
Sometimes the new file contains only small parts of the original file, and very often it hits the error circle telling me, renaming was not successful. → error (“Rename fc failed”)
I am pretty sure, it has to do with the fc in the file.rename function of the SdFat library, but i have no clue, what i am doing wrong. :~
Would anybody please be so kind and tell me, what i am doing wrong and how i can solve the problem ??? :relaxed:
Here’ s the code of the function:
the sel_SD function is setting the 4 and 10 pins to either Ethernet or SD. That works too.

void rename_sdfile()
{      byte i=0;    
       byte i1=0;    
       char fc[11];
      
      delay(10);  
      sel_SD();  //sets pin to sd mode
      delay(10);
  
      fc[0]=month1/10+'0';  // writes a fc char containing mmddhhss.csv
      fc[1]=month1%10+'0';
      fc[2]=day1/10+'0';
      fc[3]=day1%10+'0';
      fc[4]=hour1/10+'0';
      fc[5]=hour1%10+'0';
      fc[6]=min1/10+'0';
      fc[7]=min1%10+'0';
      fc[8]='.';   
      fc[9]='c';
      fc[10]='s';
      fc[11]='v';     
/*      for (i=0;i<=11;i++)
      { Serial.print(fc[i]);};
      Serial.println(" ");
*/      
     

       SdFile file("DL_SC.CSV", O_RDWR );  // the basic file where all the data is written during measuring the sensors, which has to be renamed
       if (!file.isOpen()) error("DL_SC.CSV");  // error when it does not exist
       file.rewind();
       file.close();

      if (!file.rename(sd.vwd(), fc)) error("Rename fc failed");  

      file.close();
      sd.remove("DL_SC.CSV");  // remove old file

First thing to check is that your name is actually valid.

It appears you are trying to access element [11] of an array with 11 elements. That wont work in C, your array elements are 0,1,2,3,4,5,6,7,8,9 and 10 only.

So you need to make the array longer by 2 chars, and put a '\0' ( zero byte ) in the last element.

michinyon: put a '\0' ( zero byte ) in the last element.

The array certainly has to be big enough, but the null terminator is critical.

You could clean up that code considerably by using snprintf() to generate the new name, e.g.:

char fc[32]; // make it big enough to hold your longest string
snprintf(fc, sizeof(fc), "%02d%02d%02d%02d.csv", month1, day1, hour1, min1);
// this assumes that month1, day1, hour1, min1 are ints

Hello, thanks for your effort - of course an array from 0 - 11 has 12 members !! ( and not 11 - me stupid) - but after having corrected that, it still doesn't work - I keep on getting the error code for not being able to rename the file . Any other suggestions ?

I keep on getting the error code for not being able to rename the file .

With this code?

      if (!file.rename(sd.vwd(), fc)) error("Rename fc failed");

That looks like you are trying to rename a directory (that's what sd.vwd() returns) to a file name.

Problem solved ! if the file already exists - the rename function returns an error. So the file must be deleted and then it runs fine

if (!file.rename(sd.vwd(), newfc)) 
      { 
        Serial.print(newfc);
        Serial.print("  exists already ");
        sd.remove(newfc);  // remove file   
        delay(10);
        if (!file.rename(sd.vwd(), newfc)) error("Still problems with newfc");
      }

kuma77: Hello, thanks for your effort - of course an array from 0 - 11 has 12 members !! ( and not 11 - me stupid) - but after having corrected that, it still doesn't work - I keep on getting the error code for not being able to rename the file . Any other suggestions ?

Actually, it needs 13 members to hold the trailing NULL as well. As PeterH said, it's as well to make it several characters larger than the absolute minimum.