Using SD filenames derived in a program

My SD shield is working OK with the examples in the SD library but now I'm stuck!

The code

myFile = SD.open("test.txt",FILE_WRITE);

Works fine but I don't want to use a fixed literal filename.

This code won't compile:-

#include <SD.h>

File myFile;
String filename="";
int i=5;
.
.
.
.

  filename += String(i);
filename += ".csv"; 
Serial.print("created filename is ");
Serial.println(filename);

  myFile = SD.open(filename,FILE_WRITE);

By omitting the last line, I have proved that the filename <5.csv> is generated and I can print it out.

The compiler responds

ReadWrite.ino: In function 'void setup()':
ReadWrite:54: error: no matching function for call to 'SDClass::open(String&, int)'
C:\Documents and Settings\RCM\Desktop\arduino-1.0.5\libraries\SD/SD.h:74: note: candidates are: File SDClass::open(const char*, uint8_t)

Can you help me with the correct syntax please

TIA

Bob

Can you help me with the correct syntax please

There is no correct syntax for using a String as a file name. You should use a char array, and sprintf() to populate it.

Thanks,

I've got rid of String

OK code modified now to

#include <SD.h>
#include <stdio.h>
File myFile;
char filename[9]="";//plenty large enough for now
int i=5;

.
.
.
.
.

sprintf(filename,i,".csv");

I'm not familiar with sprintf but copied the syntax from elsewhere

Now the compiler does not like

Please could you give me an example of correct use of sprintf please.

TIA

Bob

I'm not familiar with sprintf but copied the syntax from elsewhere

without bothering to look up how to use it...

sprintf(filename,i,".csv");

The first argument is where to write (correct).
The second argument is the format specifier (how to format the string) - that doesn't look like a format specifier to me.
The third, and subsequent, argument(s) are the variables to be formatted. ".csv" is not a variable.

sprintf(filename, "%d.csv", i);

Thank you Paul.

NB the only book i have does not have sprintf referred to in it hence my copying from elsewhere.

Bob

NB the only book i have does not have sprintf referred to in it hence my copying from elsewhere.

I'm pretty sure that you have a computer, though, with internet access. Google knows a lot.

This might help. It uses the date as a filename but the principle is the same. You start with a char, which is essentially a dummy of fixed length, and alter it as required.

I don't understand sprintf either.

#include <SD.h>
#include "RTClib.h"
#include <Wire.h>
#include <string.h>

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

void setup()
{
Serial.begin(9600);
Wire.begin(); //Important for RTClib.h
RTC.begin();

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
return;
}

Serial.print("Initializing SD card...");

pinMode(10, OUTPUT);

if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}

void loop()
{

getFileName();
createFileName();
delay(3000);
}

void getFileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}

void createFileName(){
myFile = SD.open(filename, FILE_WRITE);
myFile.close();
}

Nick that is very helpful indeed.
I was building up to using the date as a file name but starting off easily by using a single digit.
Your code will not only leapfrog me onto the next step of collating and formatting to write to the file of the day but you have also pointed me towards a different RTC library.

I'm a little worried about code size already though. With RTC and SD libraries just creating an empty file, I'm 16k through my 32k of program RAM
I eventually will need one wire, dallas temp sensor and Ethernet libraries functioning as well as some code to do something useful like push the data onto the web.

Thanks for your help

Bob

9fingers:
I eventually will need one wire, dallas temp sensor and Ethernet libraries functioning as well as some code to do something useful like push the data onto the web.

LoL....... In that case, now is probably a good time to start thinking of getting a Mega. Trust me, if you are talking like that, you will hit the 32k wall sooner than you think and, if anybody tries to tell you otherwise, just offer them your code. The subsequent silence is likely to be deafening......

It's pretty clear that you are looking in much the same direction as me:

3x DS18B20s plus a water meter, plus derived calculations
factors in those calcs vary according to time of day
local display every second, LCD and bluetooth
public display on the internet every ten seconds
local backup to SD with timestamp every ten seconds
datafile renewed with new name daily
daily resume on separate file
etc. etc.......?

It's the libraries that get you and there really isn't any escape. The above takes fourteen libraries.

In the matter of the clocks, my project currently uses no library for the actual timekeeping but I am obliged to use RTC.lib for the filename section. This sounds dumb but I have not been able to improve on matters by using the library to tell the time. I believe this might be because the compiler only uses what it needs from a library, and is pretty efficient about it. I have only just gotten something started with the daily variables. This involves two more time libraries and I don't yet know if I can modify the date-as-filename section to use one of those.

Looks like a very similar plan to mine. This afternoon I'll be merging the code to run the 9 temp sensors plus associated calculations with the SD and RTC stuff and see how the memory goes with that lot.
I'll possibly find myself on the 'bay tonight ordering a Mega 2560. They take about 3 weeks from HK to GB so I need to plan ahead.

Bob

9fingers:
find myself on the 'bay tonight ordering a Mega 2560.

I'm sure you won't regret doing that. I believe the SD library will be the big hit.