converting *char array to string

Hi, there! Please bear with me on this one since I tried to make this as clear as possible.

I’m working on this Arduino project that involves Adafruit Wave Shield and buttons. I simply wanted to build a simple music launchpad / drumkit with them.

So in order to do it, I planned to store the filenames inside a *char array. Why? Because the Wave Shield library only uses *char as its parameter. I could also only get the filename from the “dirBuf” register (dirBuf.name to be exact) in the SD card, which is also *char.

The problem though is that whenever I try to copy the filename directly from the SD card / dirBuf register, the period / dot from the extension file “.WAV” gets deleted but what I need is to store the exact, original filename inside my designated array for future use.

So in order to do that, I need to duplicate it to a string variable so I can replace “WAV” with “.WAV” (notice the period for comparison) since *char values are immutable.

Now, my code (as seen below) works but only for the first 4 files read and I heard String can be problematic so I’m asking for help on how to modify this code into a reliable and consistent one.

Here’s a snippet of my code:

char *temp=dirBuf.name; //declare a local *char variable so it's compatible with dirBuf.name

String toString = String(temp); //convert *char into String

toString = toString.substring(0, toString.length()-4); //remove "WAV" from the file name since it's missing a dot

toString.concat(".WAV"); //add ".WAV" to the filename to equalize it with the original filename

char *cstr = &toString[0]; //get updated string and store it in a new array of pointers

Thanks!

Fair warning: snippets are frowned upon. You may wish to edit your post to include the entire sketch. Or, if it's too long work up an MCVE. Trust me, it'll save time.

Thank you for the heads-up. Will modify and work on an MCVE asap!

  char extension[] = ".WAV";
  char *temp = (char*)malloc(strlen(dirBuf.name) + 1);
  
  memcpy(temp, dirBuf.name, strlen(dirBuf.name));
  memcpy(temp + (strlen(dirBuf.name) - 3), extension, strlen(extension));

  Serial.println(temp);