Strange concatenation with toCharArray

I am trying to build an incremental file name by first making the string, then calling toCharArray, which works for one file, but when I try to make a second file, the first file name gets the second file name added to it and it throws a write error.

Here is the code and the subsequent serial output:

#include <EEPROM.h>

const int progadd = 80;
byte programver;
String filename = "log";
String errorfile = "err";
char filenameArray[10];
char errorArray[10];

void setup() {
  Serial.begin(9600);
  programver = EEPROM.read(progadd);
  String Sprogramver = String(programver);
  if (programver < 100) {
    Sprogramver = "0" + Sprogramver;
  }
  if (programver < 10) {
    Sprogramver = "0" + Sprogramver;
  }
  filename += Sprogramver;
  filename += ".txt ";
  errorfile += Sprogramver;
  errorfile += ".txt ";
  filename.toCharArray(filenameArray, 11);
  Serial.print("filenameArray: ");
  Serial.println(filenameArray);
  errorfile.toCharArray(errorArray, errorfile.length());
  Serial.print("filenameArray2: ");
  Serial.println(filenameArray);
  Serial.print("errorArray: ");
  Serial.println(errorArray);
}


void loop() {
  
}

With output:
filenameArray: log075.txt
filenameArray2: log075.txterr075.txt
errorArray: err075.txt

As you can see, the filename is fine until I call toCharArray on errfile.

Do I need to clear some bits somewhere? What makes it more confusing is I set the length of the filename array to 11 manually, so I don't understand how it will hold more chars.

Thanks!

  filename.toCharArray(filenameArray, 11);

You've defines filenameArray to hold 10 characters. Why are you lying to this function, telling it that the array can hold 11 characters PLUS a terminating NULL?

When I ran it with 10, it dropped off the last 't', so I changed it to 11. I also set it to filename.length() and added a blank space after ".txt" and it works on it's own. The other way that I've done it is filename.length() + 1.

When I ran it with 10, it dropped off the last 't', so I changed it to 11.

So, you told it that it was OK to write beyond the end of the array. I wonder why that didn't work.

I also set it to filename.length() and added a blank space after ".txt"

The size of something else is the WRONG input.

The other way that I've done it is filename.length() + 1.

Maybe you should try your shoe size, instead. If that doesn't work, perhaps your sleeve length.

Make the array bigger. Supply the size of the array as the second argument to the toCharArray() method. That is the ONLY process that will work.

Maybe you should try your shoe size, instead.

Hey, great advice! I set both array sizes to my shoe size (12) and made the second argument in the toCharArray() method 12, and it worked!

I am still confused as to why it has to be 12, even with a stop bit. There are 10 characters in the name, so does this just chop off the last character?

I am still confused as to why it has to be 12, even with a stop bit. There are 10 characters in the name, so does this just chop off the last character?

The second argument tells it how much space there is in the array, NOT the number of bytes to extract. The method already knows how many characters there are in the String. The function will extract all of them, if that number is smaller than the destination array size. If it is not, it will truncate the String.

There are 10 characters in the name. With the terminating NULL, the array size must be at least 11. Good thing you don't have small feet. 8)

Shouldn't you have defined a constant for that? Your shoe size isn't going to change.

With the terminating NULL, the array size must be at least 11.

Oh, I see. The array is 11, so the second argument has to be 11 + 1. Thanks!

Shouldn't you have defined a constant for that?

Do you mean a variable? All of the file names are the same length since I add leading zeros.

Oh, I see. The array is 11, so the second argument has to be 11 + 1. Thanks!

NO! If the size of the array is 11, the second argument MUST be 11 or less. NOT more.