Random Character Being Added to Char Array Unintentionally

I am using a char array to allow users to input the name of a file they would like to remove from an SD card. Other programs that I have written have been working fine, but this one has a nasty habit of adding either an 'f' or a '5' to the end of the character array after I use it to open a file (as shown by the 3rd hash mark). As far as I know, the array shouldn't be getting modified at all at that point, so I am a little lost. The lines I added for debugging purposes are denoted by "/////." The source code is below. Thanks!

FYI: While the code may have some extraneous operations, that is only because it is an excerpt from a much larger program.

#include <SPI.h>
#include <SD.h>
Sd2Card card;
SdVolume volume;
SdFile root;

String userInput;

void setup() {
  
  Serial.begin(9600);
  Serial.setTimeout(4500);
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  if (!SD.begin(10)) {
  Serial.println("failed to find SD card");
  }
  Serial.println("File name:");
  userInput = Serial.readString();
              Serial.println(userInput);                                   /////
              userInput.trim();
              userInput.concat(".TXT");
              char fileName[userInput.length()];
              userInput.toCharArray(fileName, userInput.length() + 1);
              Serial.println(fileName);                                    /////
              File userFile = SD.open(fileName  , FILE_WRITE);
              Serial.println(fileName);                                    /////
              userFile.close();
  if (SD.exists(fileName)) {
  SD.remove(fileName);
    if (!SD.exists(fileName)) {
      Serial.println("File removed.");
    }
  }
  Serial.println(fileName);
}

void loop(){
  ;
}
char fileName[userInput.length()];

Wrong. If the user entered "Stupid", and you concatenated .TXT, then the length will be 10, but you need room in the array for 10 characters AND the terminating NULL.

userInput.toCharArray(fileName, userInput.length() + 1);

Since you have an array that can hold 10 characters, why on earth would you tell the toCharArray() method that the array can hold 11 characters?

Please read the post at the top of this Forum by Nick gammon titled Read this Before Posting... for the proper way to post source code here.

@econjack fixed

asdff01:
@econjack fixed

Was he broke?

Did you fix the issues I pointed out?

@PaulS Just implemented the fix and it works! Thanks for the catch. I'm curious as to why it worked when used earlier in the same program to find and edit/read files, but only broke once I tried to remove them. Anyway, thanks for your help.

I'm curious as to why it worked when used earlier in the same program to find and edit/read files, but only broke once I tried to remove them.

When you write beyond the end of an array you own, you crap on memory some other array/code owns. Unpredictable things happen. You might not notice that you crapped on someone else's space. Or, you might. You got lucky once. Don't depend on getting lucky again.