SD card variable file name with SdFat.h

Hello

My final project is to use an inteface created with windows form to choose the filename. Here is an example of code but I can't open file with variable filename ...

#include <SPI.h>
#include "SdFat.h"

SdFat SD;

#define SD_CS_PIN PB12

File myFile;
char filename[] = "filename.txt";

void setup() {
  Serial1.begin(115200);
  Serial1.print("Initializing SD card");
  SPI.setModule(2);
  if (!SD.begin(SD_CS_PIN)) {
    Serial1.println("initialization failed!");
    while(1);
  }
  Serial1.println("initialzation done");
      
  myFile = SD.open(filename, FILE_WRITE);
  
}

void loop() {
  if (myFile) {
    myFile.println("hello");
  } else {
    Serial1.println("Failed to write on SD card !!!");
  }
  myFile.close();
}

Someone can help me ?

Thanks

Pierre

(deleted)

yes I have already followed several tutorials but without success.

it works with this code :

myFile = SD.open("IMU3.txt", FILE_WRITE);

but I want to be able to change the filename with soft on computer that communicates with the serial port.
I use an STM32f103c8 board

(deleted)

If I try to compile your sketch, the compiler rejects this line:

  SPI.setModule(2);

Hello,

It's because I use an STM32F103C8 board. It's must be different with arduino board