Issue reading and writing file on a USB flash drive

I have a program with two functions: Lettura_file_config() ,

void Lettura_file_config() {

  pinMode(PA_15, OUTPUT);  // abilita la porta USB-A
  digitalWrite(PA_15, HIGH);

  while (!Serial)
    ;

  delay(2500);

  while (!msd.connect()) {
    delay(1000);
  }

  Serial.println("Montaggio del dispositivo USB...");
  int err = usb.mount(&msd);
  if (err) {
    Serial.print("Errore nel montaggio del dispositivo USB ");
    Serial.println(err);
    while (1)
      ;
  }

  // Leggi il file e assegna i valori alle variabili
  Serial.println("Apertura del file...");
  FILE *f = fopen("/usb/config_file.txt", "r");
  if (f) {

    // Leggi e assegna il valore della prima riga a SSD */
    SSD = readLineFromFile(f);

    // Leggi e assegna il valore della seconda riga a SSD_PASSWORD
    SSD_PASSWORD = readLineFromFile(f);

    // Chiudi il file
    fclose(f);

    // Stampa i valori letti
    Serial.println("Valori letti dal file:");
    Serial.print("SSD: ");
    Serial.println(SSD);
    Serial.print("SSD_PASSWORD: ");
    Serial.println(SSD_PASSWORD);
  } else {
    Serial.println("Impossibile aprire il file.");
  }
  fclose(f);
}

used to read a file on the USB flash drive,

and another function salva_datalog()

digita ovoid salva_datalog() {
  Serial.println("Tentativo di connessione all'unità USB...DATALOG");

  // Aspetta fino a quando l'unità USB è montata correttamente
  while (!msd.connect()) {
    delay(1000);
    Serial.println("In attesa di connessione all'unità USB DATALOG...");
  }

  Serial.println("Montaggio del dispositivo USB... ");

  // Monta l'unità USB
  int err = usb.mount(&msd);
  if (err) {
    Serial.print("Errore nel montare il dispositivo USB DATALOG: ");
    Serial.println(err);
    return;  // Esci dalla funzione se c'è un errore
  }

  Serial.println("Apertura del file...");
  FILE *f = fopen("/usb/datalog.txt", "w+");
  if (!f) {
    Serial.println("Errore nell'apertura del file.");
    return;  // Esci dalla funzione se c'è un errore
  }

  // Scrivi i dati nel file
  for (int i = 0; i <= 11; i++) {
    fprintf(f, "%d\n", Totale_scatti[i]);
  }

  // Chiudi il file
  fclose(f);
  Serial.println("File data_log chiuso.");
} incolla il codice qui

which writes a log file onto the USB flash drive.

The problem arises when you execute both functions: the second function always returns error -22.
This occurs regardless of the execution order.
However, it does not happen when you execute only one function.
Do you have any ideas?

I know in the one sketch that I used to read files from USB drive, I had code in it like:

      if (!msd.connected()) {
        if (msd.connect()) Serial.println("MSD Connect");
      }
      if (msd.connected()) {

That is, you don't try to do a connect if the drive is already connected.

You might try something like that to see if that helps or not.

"OK! I thought it was something like that! At this point, since the function that writes the file is always executed after reading the file, and reading the file only occurs when Arduino is powered on, I could modify the function in this way:"

void salva_datalog() {

  Serial.println("Apertura del file...");
  FILE *f = fopen("/usb/datalog.txt", "w+");
  if (!f) {
    Serial.println("Errore nell'apertura del file.");
    return;  // Esci dalla funzione se c'è un errore
  }

  // Scrivi i dati nel file
  for (int i = 0; i <= 11; i++) {
    fprintf(f, "%d\n", Totale_scatti[i]);
  }

  // Chiudi il file
  fclose(f);
  Serial.println("File data_log chiuso.");
}

this is because the drive is already connected and i dont need to connect it again !

Tnx u!