Strange concatenation with toCharArray


i trying build incremental file name first making string, calling tochararray, works 1 file, when try make second file, first file name gets second file name added , throws write error.

here code , subsequent serial output:

code: [select]
#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:


code: [select]
filenamearray: log075.txt
filenamearray2: log075.txterr075.txt
errorarray: err075.txt


as can see, filename fine until call tochararray on errfile.

do need clear bits somewhere? makes more confusing set length of filename array 11 manually, don't understand how hold more chars.

thanks!

code: [select]
  filename.tochararray(filenamearray, 11);
you've defines filenamearray hold 10 characters. why lying function, telling array can hold 11 characters plus terminating null?


Arduino Forum > Using Arduino > Programming Questions > Strange concatenation with toCharArray


arduino

Comments