SD open issue

Status
Not open for further replies.

Fluxanode

Well-known member
Can someone tell me why this compiles ok on my 3.2

myFile = SD.open("filename", FILE_WRITE);

But this doesn't compile?

String filename = "asdf.txt";
myFile = SD.open(filename, FILE_WRITE);

Caused errors compiling.
 
The reason why is involves some history of Arduino. It's really just C++ and from the beginning everything used ordinary C strings. Even though the String C++ class was added to Arduino about 10 years ago, the reality is most libraries still only accept C type strings.

So to give a C++ String to a function which wants a C string, you need this:

myFile = SD.open(filename.c_str(), FILE_WRITE);
 
Status
Not open for further replies.
Back
Top