1
votes

I'm using the SD.h library to write onto an SD card with the Arduino Uno. I need to write out in a file a template string with some placeholder replaced by certain values, in the way of printf behaves. I would use the fprintf function, but when I tried this:

File dataFile = SD.open("myfile.txt", FILE_WRITE);
fprintf(dataFile, "mynumber: %d\n", 100);

I got this error:

cannot convert 'File*' to '__file*' for argument '1' to 'int fprintf(__file*, const char*, ...)'

How can I manage this?

1

1 Answers

3
votes

printf() makes your executable object ~1000 bytes larger, so you may not want to use it if size is a problem.

The fprintf is not intended to use with the SD.h so I think

The simple solution that comes into my mind is using sprintf to format your text then write it to the file with the println function

File dataFile = SD.open("myfile.txt", FILE_WRITE);
char text[100];
sprintf(text,"My number: %d",yournumber);
dataFile.println(text);