Arduino opens SD filename as string

I am trying to open a file that calculates the name in a string. However, it just gives me compilation errors as shown.

for(int i=1;;i++)
{
   String temp = "data";
   temp.concat(i);
   temp.concat(".csv");
   if(!SD.exists(temp))//no matching function for call to sdclass::exists(String&)
   {
      datur = SD.open(temp,FILE_WRITE);
   }
}

      

I'm a java person so I don't understand why this doesn't work. I tried several methods of string objects but none of them worked. I'm a bit new to arduino programming but I understand java much better. The point of this loop is to create a new file every time the arduino is rebooted.

+3


source to share


1 answer


SD.open

expects a character array instead String

, you need to convert it first using the method toCharArray

. Try

  char filename[temp.length()+1];
  temp.toCharArray(filename, sizeof(filename));
  if(!SD.exists(filename)) { 
   ...
  }

      

Completed code:



for(int i=1;;i++)
{
   String temp = "data";
   temp.concat(i);
   temp.concat(".csv");
   char filename[temp.length()+1];
   temp.toCharArray(filename, sizeof(filename));
   if(!SD.exists(filename))
   {
      datur = SD.open(filename,FILE_WRITE);
      break;
   }
}

      

You will find a number of functions that accept char arrays instead of strings.

+9


source







All Articles