[SOLVED] SD.remove(String) don't work , SD.remove("file.txt") work

I want to give a string as argument to SD.remove
When I try that I get

Initializing SD card..
and restart of sketch

Is this to be expected from SD.remove
In Reference and examples I can only see static text argument so I guess answer is yes.

If so, can I use some trick to do what I want ?

Ha det gött
Bosse

If you are using a String object, try this:

SD.remove(String.c_str());

remove receives what is called a "constant char pointer", aka C-style string. So String.c_str() returns a C-style string "view" of its contents.

Thanks Lucario
I tested but had same failure

Tested like this:

String t;
t="test.txt";
//SD.remove("test.txt");
delay(900);
SD.remove(t.c_str());

I found this solution working:

String t;
t = "test.txt";
char buf[10];
t.toCharArray(buf,10);
SD.remove(buf);

:slight_smile:

There's no reason to waste resources on a String if its only lot in life is to be used to generate a char array.

  char* buf = "test.txt";
  SD.remove(buf);

c-style strings are much safer on an Arduino that the String class.

Yes I kinda understand, browsing forum, Strings are not so popular.
Just had a hard time with pointers starting programming.
Thanks for the example. I'll try that out :slight_smile: