Is it possible to run the function using the value of a variable as the file name?

I am recording some data on the SD card and I want the file name where it is saved to be assigned by a function ...
For example:
String (file) = (("/mnt/sd/")+datactual+".csv");
File dataFile = (file, FILE_APPEND);
But that doesn’t work. :frowning:

Thank you for your time.

Have you investigated the preprocessor's stringify operator '#'?

It may do what you want.

Always a very useful description of the problem. Does it not compile? Or doesn't it create a file? Or something else?

Which SD library do you use? We probably wouldn't have to ask if you had provided a complete code that demonstrates the problem and if we knew which board you're using.

Note that filenames on SD cards usually have to adhere to the 8.3 notation; so if datactual contains "Hello World", you're out of luck.

Not so surprising. It is incomprehensible. For all that, using a variable as a filename is common practice, the most obvious example being the use of the date:
Just stick with DOS-type 8.3 names, as mentioned above.

thanks for the interest, but i don't know exactly what you mean, i'm not an expert, you notice right?

Thanks for the interest ....

It doesn't copy the whole program because it's quite extensive, but it works, only now I want to make a modification, so far the information is always saved in the same file and I want it to be saved in a different file every month.
Test-fitxer-SD.ino (1.7 KB)

the error message when compiling is as follows:

In file included from /home/pere/Documents/Projectes/Programes per arduino/Test-fitxer-SD/Test-fitxer-SD.ino:4:0:
/snap/arduino/61/libraries/Bridge/src/FileIO.h:83:10: note: candidate: BridgeLib::File BridgeLib::FileSystemClass::open(const char*, uint8_t)
File open(const char filename, uint8_t mode = FILE_READ);
/snap/arduino/61/libraries/Bridge/src/FileIO.h:83:10: note: no known conversion for argument 1 from 'String' to 'const char
exit status 1

The compiler is objecting to the use of a String object in that function call - it was expecting a C string instead. Take a look at the String docs in the reference - you can use c_str() to solve your problem I think.

Code something like this, whereby a new file is created when the RTC returns a new month, i.e. midnight on the first.

#include <SD.h>
char filename[] = "000000.CSV";

void setup() {
    thismonth = month; // flag for later 

void loop() {  
  if (thismonth != month)
   thismonth = month;

void getFileName(){
sprintf(filename, "%02d%02d.csv", year, month);

Thanks for the help.
Indeed this is the problem ...
You just have to add .c_str () to the variable "file"

the function "registrar_dades () is as follows:

void registrar_dades()
// El fitxer es troba a "/mnt/sd/any-mes.csv"
String(file) = (("/mnt/sd/")+datactual+".csv");
File dataFile =, FILE_APPEND);

The information on this link has also been very useful

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.