i notice that many people looks for this so here is what i wrote to write txt file...
first you may have a RTC clock or you might be using the librearies...
so it is important to have this libraries working
#include <DS1307RTC.h>
#include <Time.h>
make sure your RTC comands work well, the function below should print the date on the serial port.
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
Now the function to convert INT to STRING to name your file...
void file_name()//function to name the text files each day...
{
char file_today_name[30];//declare this function as a global..i just put it here for demostration
char s_day[9],s_month[9],s_year[9];// Variables for storing the conversion from int to string
const char zero[]="0";// Zero to add when ever the month is 9.. to be 09, so you always have the same string lenght
itoa(day(),s_day,10);// convert day to string
itoa(month(),s_month,10); //convert month to string
itoa(year(),s_year,10); //convert year to string
for(int i=0;i<30;i++)// wipe the file name to and empty string
{ // if not it will keep appending the string in the copy functions
file_today_name[i]='\0';
}
if(month()<10);strcat(file_today_name,zero);// add trailing zeros for month
strcat(file_today_name,s_month);
if(day()<10);strcat(file_today_name,zero); // add trailing zeros for day
strcat(file_today_name,s_day); // concatenate strings
strcat(file_today_name,s_year);//concatenate strings
strcat(file_today_name,".txt");// file name stored in file_today with .txt
//Serial.print(file_today_name);
}
now before writing the SD card make sure you run the function
file_name();
if (!sd.init(SPI_HALF_SPEED, 4)) sd.initErrorHalt();
// open the file for write at end like the Native SD library
if (!myFile2.open(file_today_name, O_RDWR | O_CREAT | O_AT_END))
{
stuff.........
}
this should take care of your needs to name files with the current date...