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(){
;
}