Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino opening SD filename as string

I am trying to open up a file that I calculate the name into a string. However, it is just giving me compile 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 am a java person, so I don't see why this isn't working. I tried a few string object methods but none seem to have worked. I am a bit new at arduino programming but I understand java much better. The point of this for loop is to make a new file each time the arduino reboots.

like image 695
Wijagels Avatar asked Jan 22 '13 20:01

Wijagels


1 Answers

SD.open expects a character array instead of a String, you need to convert it using the toCharArray method first. 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 take char arrays instead of strings.

like image 123
iagreen Avatar answered Oct 22 '22 09:10

iagreen