Create file name with date stamp each day

I am trying to include the current date, using the "YYYY-mm-dd.txt" format, into the file name of a data logging sketch. I would also like to check to see if an existing file exists for the current date. If so, I want the sketch to append to the existing file, otherwise I want to create the file if it does not already exist.

I've followed a handful of existing examples and have had to pick and choose and modify code to get things working (kinda) with my DS3231 real time clock module. My current version compiles successfully, but is not behaving as expected.

Here's some examples that I'm drawing from:

date as a file name example 1

date as a file name example 2

My example below successfully initializes the SD card, but fails to write the new file each loop, and then prints to the serial monitor that the current day's filename does not exist. When checking the SD card, there isn't any new file being written either. So I suspect there is something wrong with my SD.open() method.

How can I ensure that the sketch actually creates the file with the current day's file name?

This is my first post to the forum and your patience is appreciated.

Thank you!

#include <SD.h>
#include <DS1307RTC.h>
#include <string.h>
#include <SPI.h>
#include <Wire.h>
#include <TimeLib.h>


const int chipSelect = 10;  // chip select pin for teensy with SD module 
char filename[16] = {0};    // character array variable to store our converted date string for SD.open()
File dataFile;              // file object
tmElements_t tm;

void setup()
{
//initialize serial ports
  Serial.begin(9600);  
  Serial.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

  Serial1.begin(9600);         
  Serial1.setTimeout(1500);    //set the Timeout to 1500ms, longer than the data transmission periodic time of the sensor

  setSyncProvider(RTC.get);

  if(timeStatus() != timeSet)
    Serial.println("Unable to sync with RTC");
  else
    Serial.println("RTC has set the system time");

  //lines through Serial.println("card initialized") from Teensy Datalogger example
  //to initialize the SD card, which is on the Teensy Audio board
  //chipSelect should be set to 10

  Serial.print("Initializing SD card...");
  SPI.setMOSI(11);
  SPI.setMISO(12);
  SPI.setSCK(13); 
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    //while (1); // wait until card is recognized...
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  getFileName();
  createFileName();
  //Serial.println("writing file name");
  delay(1000);
}

// creates a new file name with today's date
void getFileName() {

  // store the date in this empty string
  String date = "";

  // check the day each loop
  tmElements_t tm;

 
  if (RTC.read(tm)) {

    date = String(tmYearToCalendar(tm.Year)) + "-" + 
               String(tm.Month) + "-" +
               String(tm.Day) + ".txt";

    date.toCharArray(filename, 16);

    Serial.println("filename: " + String(filename));

    //dataFile = SD.open(str, FILE_WRITE);
    }
}


// creates a new file name each day if it doesn't already exist
void createFileName() {
  //Check if file exists?
  if (SD.exists(filename)) {
    Serial.println("exists");
    Serial.println("appending to existing file");
  }
  else {
    Serial.println("doesn't exist");
    Serial.println("Creating new file.");
    Serial.println(filename);
    dataFile = SD.open(filename, FILE_WRITE);
    dataFile.close();
  }
}

// adds leading zeros to single digit month and day characters
String as2digits(int number) {
  String answer = "";
  if (number >= 0 && number < 10) {
    answer += ('0');
  }
  answer += String(number);
  return(answer);
}

Here's what my serial monitor output looks like:

RTC has set the system time
Initializing SD card...card initialized.
filename: 2018-4-25.txt
doesn't exist
Creating new file.
2018-4-25.txt
filename: 2018-4-25.txt
doesn't exist
Creating new file.
2018-4-25.txt

Look carefully at the filenames in the examples you cite.
Now look at the filenames that you create.

Do you notice any difference in the length of the filenames ?

I think that you will find that the SD library only support filenames in the 8.3 format.

Wow, it was that simple. I had glossed over the intro in the examples and went straight to focusing on the code... I'll slow down next time and read things more carefully.

I changed the filename variable declaration to char filename[12] = {0}; and things worked just fine.

One question, should my character array buffer be 13 instead of 12?

It seems to work just fine with a buffer of 12 bytes, but I was under the impression that it's a good practice to have one extra byte in a buffer in C.

Thank you for having me think through the problem a little.

I really appreciate your help!

I changed the filename variable declaration to char filename[12] = {0}; and things worked just fine.

Did you change the length of the filename to match ?

Why are you messing around with Strings, especially as you convert the String to a C style string (a zero terminated array of chars) before using it.

The array of chars used by C style strings needs a extra entry over and above the number of characters to hold the terminating zero.

porlando:
It seems to work just fine with a buffer of 12 bytes, but I was under the impression that it's a good practice to have one extra byte in a buffer in C.

Yes, just as it is good practice to fill a swimming pool with water before you dive into it.

You might not have broken any bones this time, but still...

By the way, you should get rid of the capital-S Strings. Read up on char arrays and sprintf().