SD card logging problem

Hello all,
I working on a small project using RTC DS1307. I want to save my data into SD card at a particular time (as i mentioned in below code ). My program is working correctly but i am facing problem to setup header in the SD card. In the very first line i like to print "TIME" and after that Arduino will start saving time from next line.
Am i doing right using Switch case for that or there is other solution available. Please enlighten me .
Thanks in advanced.
Ashish

#include <SD.h>
#include <DS1307.h> 

DS1307 clock;  
#define chipselect 4
#define LED 10
File datalogging;
int datalogging1;

void setup() {
 Serial.begin(9600);
  Serial.print("WELCOME");
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipselect)) {
  Serial.println("initialization failed!");
  while (1);
  }
  Serial.println("initialization done.");

  clock.begin();                                             
  clock.fillByYMD(2021,10,15);                            
  clock.fillByHMS(8,49,00);                       
  clock.fillDayOfWeek(FRI);                                
  clock.setTime();  
  Serial.print("Checking File");
  if(SD.exists("logging.txt")){
    
    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }
  Serial.println("Removing logging.txt...");
  SD.remove("logging.txt");
  
  if (SD.exists("logging.txt")) {
    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }
    
    datalogging = SD.open("datalogging.txt", FILE_WRITE);
    datalogging.close();
 datalogging1=0;
 Datalogging();
}


void loop() {

 rtctime();
 if(clock.hour==8 && clock.minute==50){
  datalogging1=1;
   Datalogging();
 }
}
  void Datalogging(){
    switch (datalogging1){
      case 0: 
    datalogging = SD.open("datalogging.txt", FILE_WRITE);
    if(datalogging){
    datalogging.print("Timer (s)");
    datalogging.print("\n");
    datalogging.close();
Serial.println("logging Finished");
 
  }
  else{
    Serial.println("Error Opening file");
    }
    break;
    case 1: 
    Serial.println("logging into file");
  datalogging= SD.open("logging.txt", FILE_WRITE);
  if(datalogging){
    
    datalogging.print(clock.hour);
    datalogging.print(":");
    datalogging.print(clock.minute);
    datalogging.print(":");
    datalogging.print("00");
    datalogging.print("\n");
    datalogging.close();
Serial.println("logging Finished");
  }

  else{
    
    Serial.println("Error Opening loggigng file");

  }
  break;

    }
  }
     


void rtctime(){
 clock.getTime();
  Serial.print("TIME-");
  Serial.print(clock.hour, DEC);
  Serial.print(":");
  Serial.print(clock.minute, DEC);
  Serial.print(":");
  Serial.println(clock.second, DEC);
  Serial.print("  ");
  Serial.print("DATE-");
 Serial.print(clock.month, DEC);
  Serial.print("/");
 Serial.print(clock.dayOfMonth, DEC);
 Serial.print("/");
  Serial.print(clock.year+2000, DEC);
  Serial.println(" ");
  switch (clock.dayOfWeek)
  {
    case MON:
    Serial.print("MON");
    break;
    case TUE:
    Serial.print("TUE");
    break;
    case WED:
    Serial.print("WED");
    break;
    case THU:
    Serial.print("THU");
    break;
    case FRI:
    Serial.print("FRI");
    break;
    case SAT:
    Serial.print("SAT");
    break;
    case SUN:
   Serial.print("SUN");
    break;
  }
  Serial.print(" ");
  return;
  
}

At the moment your code is small. So you might think it is sufficient to use constants like "0" and "1" for your switch-case.

If your code grows in the development-process the code will become more complex and then everything should have self-explaining names

In case of your switch-case structure this means to define constants that have a self-explaining name
"0" says almost nothing writeHeader says it clear
"1" says almost nothing appendData says it clear

datalooging1 does not say very clear what the function of the variable is
a name like fileActionState says it very clear function control what kind of file-action shall be done suffix "State" it is a variable used as state-variable in a switch-case-statement.

You have to open the file in the FILE_APPEND-mode to add new data at the end of the file

here is a modified code-version. Your original code is still there as a comment

#include <SD.h>
#include <DS1307.h>

DS1307 clock;
#define chipselect 4
#define LED 10
File datalogging;
byte fileActionStateState; // int datalogging1;

const byte writeHeader = 0;
const byte appendData  = 1;



void setup() {
  Serial.begin(9600);
  Serial.print("WELCOME");
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipselect)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  clock.begin();
  clock.fillByYMD(2021, 10, 15);
  clock.fillByHMS(8, 49, 00);
  clock.fillDayOfWeek(FRI);
  clock.setTime();
  Serial.print("Checking File");
  if (SD.exists("logging.txt")) {

    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }
  Serial.println("Removing logging.txt...");
  SD.remove("logging.txt");

  if (SD.exists("logging.txt")) {
    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }

  datalogging = SD.open("datalogging.txt", FILE_WRITE);
  datalogging.close();
  fileActionState = writeHeader; // datalogging1 = 0;
  Datalogging();
}


void loop() {

  rtctime();
  if (clock.hour == 8 && clock.minute == 50) {
    fileActionState = appendData; //datalogging1 = 1;    
    Datalogging();
  }
}


void Datalogging() {
  switch (fileActionState) {//switch (datalogging1) {
    case writeHeader: // case 0
      datalogging = SD.open("datalogging.txt", FILE_WRITE);
      if (datalogging) {
        datalogging.print("Timer (s)");
        datalogging.print("\n");
        datalogging.close();
        Serial.println("logging Finished");

      }
      else {
        Serial.println("Error Opening file");
      }
      break;
      
    case appendData: // case 1:
      Serial.println("logging into file");
      datalogging = SD.open("logging.txt", FILE_APPEND); //datalogging = SD.open("logging.txt", FILE_WRITE);
      if (datalogging) {

        datalogging.print(clock.hour);
        datalogging.print(":");
        datalogging.print(clock.minute);
        datalogging.print(":");
        datalogging.print("00");
        datalogging.print("\n");
        datalogging.close();
        Serial.println("logging Finished");
      }

      else {

        Serial.println("Error Opening loggigng file");

      }
      break;

  }
}



void rtctime() {
  clock.getTime();
  Serial.print("TIME-");
  Serial.print(clock.hour, DEC);
  Serial.print(":");
  Serial.print(clock.minute, DEC);
  Serial.print(":");
  Serial.println(clock.second, DEC);
  Serial.print("  ");
  Serial.print("DATE-");
  Serial.print(clock.month, DEC);
  Serial.print("/");
  Serial.print(clock.dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(clock.year + 2000, DEC);
  Serial.println(" ");
  switch (clock.dayOfWeek)
  {
    case MON:
      Serial.print("MON");
      break;
    case TUE:
      Serial.print("TUE");
      break;
    case WED:
      Serial.print("WED");
      break;
    case THU:
      Serial.print("THU");
      break;
    case FRI:
      Serial.print("FRI");
      break;
    case SAT:
      Serial.print("SAT");
      break;
    case SUN:
      Serial.print("SUN");
      break;
  }
  Serial.print(" ");
  return;

}

best regards Stefan

Thank you so much Stefan for Correcting me. there are few errors in the code. I've corrected it.

#include <SD.h>
#include <DS1307.h>

DS1307 clock;
#define chipselect 4
#define LED 10
File datalogging;
byte fileActionState; // int datalogging1;

const byte writeHeader = 0;
const byte appendData  = 1;



void setup() {
  Serial.begin(9600);
  Serial.print("WELCOME");
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipselect)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  clock.begin();
  clock.fillByYMD(2021, 10, 15);
  clock.fillByHMS(8, 49, 00);
  clock.fillDayOfWeek(FRI);
  clock.setTime();
  Serial.print("Checking File");
  if (SD.exists("logging.txt")) {

    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }
  Serial.println("Removing logging.txt...");
  SD.remove("logging.txt");

  if (SD.exists("logging.txt")) {
    Serial.println("logging.txt exists.");
  } else {
    Serial.println("logging.txt doesn't exist.");
  }

  datalogging = SD.open("datalogging.txt", FILE_WRITE);
  datalogging.close();
  fileActionState = writeHeader; // datalogging1 = 0;
  Datalogging();
}


void loop() {

  rtctime();
  if (clock.hour == 8 && clock.minute == 50) {
    fileActionState = appendData; //datalogging1 = 1;    
    Datalogging();
  }
}


void Datalogging() {
  switch (fileActionState) {//switch (datalogging1) {
    case writeHeader: // case 0
      datalogging = SD.open("logging.txt", FILE_WRITE);
      if (datalogging) {
        datalogging.print("Timer (s)");
        datalogging.print("\n");
        datalogging.close();
        Serial.println("logging Finished");

      }
      else {
        Serial.println("Error Opening file");
      }
      break;
      
    case appendData: // case 1:
      Serial.println("logging into file");
      datalogging = SD.open("logging.txt", FILE_WRITE); //datalogging = SD.open("logging.txt", FILE_WRITE);
      if (datalogging) {

        datalogging.print(clock.hour);
        datalogging.print(":");
        datalogging.print(clock.minute);
        datalogging.print(":");
        datalogging.print("00");
        datalogging.print("\n");
        datalogging.close();
        Serial.println("logging Finished");
      }

      else {

        Serial.println("Error Opening loggigng file");

      }
      break;

  }
}



void rtctime() {
  clock.getTime();
  Serial.print("TIME-");
  Serial.print(clock.hour, DEC);
  Serial.print(":");
  Serial.print(clock.minute, DEC);
  Serial.print(":");
  Serial.println(clock.second, DEC);
  Serial.print("  ");
  Serial.print("DATE-");
  Serial.print(clock.month, DEC);
  Serial.print("/");
  Serial.print(clock.dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(clock.year + 2000, DEC);
  Serial.println(" ");
  switch (clock.dayOfWeek)
  {
    case MON:
      Serial.print("MON");
      break;
    case TUE:
      Serial.print("TUE");
      break;
    case WED:
      Serial.print("WED");
      break;
    case THU:
      Serial.print("THU");
      break;
    case FRI:
      Serial.print("FRI");
      break;
    case SAT:
      Serial.print("SAT");
      break;
    case SUN:
      Serial.print("SUN");
      break;
  }
  Serial.print(" ");
  return;

}