concatenate two array Arduino

Hi friends,
I want to concatenate two char array, because I want to define the name of file that I have to save in SD in dynamic way.
I post my code:

void saveTotSD(){
  char *name_mese[12]={
    "Gennaio.txt","Febbraio.txt","Marzo.txt","Aprile.txt","Maggio.txt","Giugno.txt","Luglio.txt","Agosto.txt","Settembre.txt","Ottobre.txt","Novembre.txt","Dicembre.txt"            }; 
  char *name_anno[5]={
    "2015/","2016/","2017/","2018/","2019/","2020/"}; 
  byte anno_corrente = (int) year - 15;
  char path[1]; // = {name_mese[(int) month - 1]};
  sprintf(path, "%s%s", name_anno[anno_corrente], name_mese[(int) month - 1]);

  int filesavedold;
  int filesaved;
  
  if ( SD.exists(path) )
  {
    SD.remove(path);
    Serial.print("Rimosso!");
  }

  myFiletotale = SD.open(path, FILE_WRITE);
  delay(100);

  filesavedold = myFiletotale.size();

  // if the file opened okay, write to it:
  if (path) {
    myFiletotale.print("Utente1: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(0), 0));
    myFiletotale.print("Utente2: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(4), 1));
    myFiletotale.print("Utente3: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(8), 2));
    myFiletotale.print("Utente4: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(12), 3));
    myFiletotale.print("Utente5: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(16), 4));
    myFiletotale.print("Utente6: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(20), 5));
    myFiletotale.print("Utente7: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(24), 6));
    myFiletotale.print("Utente8: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(28), 7));
    myFiletotale.print("Utente9: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(32), 8));
    myFiletotale.print("Utente10: ");
    myFiletotale.println(convertSecondtoHour(EEPROM_readInt(36), 9));
    filesaved = myFiletotale.size();
    // close the file:
    myFiletotale.close();
  } 
  else {
    // if the file didn't open, print an error:
    Serial.print("error opening ");
    Serial.println(path);
  }
  if(filesaved > filesavedold){
    lcd.setCursor(0,2);
    lcd.print("                    ");
    lcd.setCursor(0,3);
    lcd.print("                    ");
    lcd.setCursor(0,2);
    lcd.print("OK, salvato");
    delay(500);
    lcd.setCursor(0,2);
    lcd.print("                    ");
    lcd.setCursor(0,3);
    lcd.print("                    ");
  } 
  else {
    lcd.setCursor(0,2);
    lcd.print("Errore salvataggio! ");
    lcd.setCursor(0,3);
    lcd.print("                    ");
    delay(3000);
  }
}

But it doesn't work. It doesn't create the file.

Your code is incomplete, so cannot compile.

char path[1];

Since when did a 12 or so chars in to 1?

Mark

What happens if:

  myFiletotale = SD.open(path, FILE_WRITE);

the call to the open() method fails? Usually it's a good idea to check myFileTotale to see if it is non-zero.

rossotony88:
I post my code:

Wasteful in defining constants. Hopefully this is code for a DUE.

And of course, the path must contain enough characters to take the whole 'path' string.

void setup()
{
  Serial.begin(9600);
  char *name_mese[12]={"Gennaio","Febbraio","Marzo","Aprile","Maggio","Giugno","Luglio","Agosto","Settembre","Ottobre","Novembre","Dicembre"}; 
  char path[19]; 
  
  int anno_corrente,month;
  for (anno_corrente=2015;anno_corrente<=2020;anno_corrente++)
    for (month=1;month<=12;month++)
    {
      snprintf(path, sizeof(path), "%4d/%s.txt", anno_corrente, name_mese[month-1]);
      Serial.println(path);
    }
}

void loop()
{
}

If it is not for a DUE but for an 8-bit Atmega, I'd better think of another solution ("PROGMEM") to save the RAM that is eaten up by the month names.

No I use Arduino Mega, but I have changed my code like you suggest but doesn't work. Infact it doesn't create a folder and file. I post all my code here.

progetto_pozzo.ino (41.8 KB)

rossotony88:
No I use Arduino Mega, but I have changed my code like you suggest but doesn’t work. Infact it doesn’t create a folder and file. I post all my code here.

FAT 8.3 filenames cannot start with space characters.

This code would create a path that starts with two leading space characters:

int year = 15;
snprintf(path, sizeof(path), "%4d/%s.txt", year, name_mese[month-1]);

You’d have to use 4-digit years instead, like:

int year = 2015;
snprintf(path, sizeof(path), "%4d/%s.txt", year, name_mese[month-1]);

Or you could add leading zeros:

int year = 15;
snprintf(path, sizeof(path), "%04d/%s.txt", year, name_mese[month-1]);

Or you could print the year with less digits:

int year = 15;
snprintf(path, sizeof(path), "%d/%s.txt", year, name_mese[month-1]);

But I think, FAT 8.3 path names are never allowed to start with space characters.

Ok, thanks I will try tonight and I let you know

Nothing, it doesn't work. If I print "path" I read the correct name. But it doesn't create a file.
Why?

rossotony88:
Nothing, it doesn't work. If I print "path" I read the correct name. But it doesn't create a file.
Why?

Who knows?
You're the one with the code that you don't want to share.

AWOL:
You're the one with the code that you don't want to share.

He did post his code meanwhile.

See file attachment attached to reply #5.

Infact, however it works ;D thank you, the problem was the folder, I had to create before.
Now I have another problem, I use this subfuction to convert second in the format hh:mm:ss i post it:

char convertSecondtoHour(int hourtosecond, int user_pos){
  double S;
  double M;
  double H;
  double temp;
  unsigned long int ore;
  unsigned long int minuti;
  unsigned long int secondi;
  //char tempototale[20];

  int Mint;
  int Hint;

  temp = (double) hourtosecond;

  M = temp/60;
  H = M/60;
  Hint = (int)H;
  ore = Hint;
  minuti = M - Hint*60;
  Mint = (int)M;
  secondi = temp - Mint*60;
  lcd.setCursor(0,2);
  lcd.print("Utente ");
  lcd.print(user_pos+1);
  lcd.print("     ");
  lcd.setCursor(0,3);
  lcd.print(ore);
  lcd.print(":");
  lcd.print(minuti);
  lcd.print(":");
  lcd.print(secondi);
  lcd.print("        ");
  Serial.print("Utente ");
  Serial.print(user_pos+1);
  Serial.print(": ");
  Serial.print(ore);
  Serial.print(":");
  Serial.print(minuti);
  Serial.print(":");
  Serial.println(secondi);
  snprintf(tempototale, 8, "%02d:%02d:%02d", ore, minuti, secondi);
  Serial.print("Eccoti:");
  Serial.println(tempototale);
  return tempototale[8];
}

When I print:

  Serial.print("Utente ");
  Serial.print(user_pos+1);
  Serial.print(": ");
  Serial.print(ore);
  Serial.print(":");
  Serial.print(minuti);
  Serial.print(":");
  Serial.println(secondi);

It works good, but when I use snprintf and I print "tempototale" It write '00:00:00'. Why?

  double S;
  double M;
  double H;

So, you can have 3.14 hours, 45.2 minutes and 42.153940 seconds? WTF?

  temp = (double) hourtosecond;

Why do you sensibly pass the value as an int and then stupidly cast it to a float?

It works good, but when I use snprintf and I print "tempototale" It write '00:00:00'. Why?

Because the format specifier MUST match the variable type. %d is for ints, not longs.

Try this and see what you get.

// to convert seconds to HH:MM:SS format
long seconds = 86397; // or however many seconds
long leftover = seconds;
char[] timestr = "00:00:00"; // our answer will go here
// array indices: 012345678
// do the conversion
// hours
while (leftover >= 36000) { leftover -= 36000; timestr[0]++; }
while (leftover >= 3600)  { leftover -= 3600;  timestr[1]++; }
// minutes
while (leftover >= 600)   { leftover -= 600;   timestr[3]++; }
while (leftover >= 60)    { leftover -= 60;    timestr[4]++; }
// seconds
while (leftover >= 10)    { leftover -= 10;    timestr[6]++; }
timestr[7] += leftover;
// we're finished converting
// the answer is now in timestr