Convert String to const char * type using Arduino

I am using the Arduino library. I would like to register some data from the sensor, date and time, in order to write it to the SD card.

To create a text file name, I tried

String dataFileName = String(String(sedClock.getTime().year(),DEC) + String(sedClock.getTime().month(),DEC) + String(sedClock.getTime().day(),DEC) + String(sedClock.getTime().hour(),DEC) + String(sedClock.getTime().minute(),DEC) + String(sedClock.getTime().second(),DEC) + '_log.txt'); 

Then I would like to enter this file using

  pinMode(SD_PIN,OUTPUT); dataFile = SD.open(dataFileName,FILE_WRITE); 

But I get

  no matching function call to SDClass::open(String&, int) candidates are: File SDClass::open(const char*,uint_8) 

But it seems that the Arduino string has no equivalent

  (const char *) dataFileName.c_str() 

So I can’t figure out how to do the right conversion

Any help would be greatly appreciated.

+7
source share
2 answers

Thank you for your help. The decision was

  char __dataFileName[sizeof(dataFileName)]; dataFileName.toCharArray(__dataFileName, sizeof(__dataFileName)); pinMode(SD_PIN,OUTPUT); dataFile = SD.open(__dataFileName,FILE_WRITE); 
+14
source

All Articles