Sketch creates new logfiles when opening serial monitor?

Hi everyone,
thanks to user @UKHeliBob I was able to get my sketch working (almost). There is only one problem which I don't understand: the sketch creates a new logfile every time I open the serial monitor.
This is the code I used:

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <RTClib.h>
#include "DHT.h"

#define DHTPIN 5
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
int hum;
int temp;
RTC_DS1307 rtc;
const int chipSelect = 10;
File logfile;
enum states
{
  FLUSHING,
  MEASURING
};

void setup()
{
  Serial.begin(9600);
  while (!Serial);

  pinMode(DHTPIN, INPUT);
  dht.begin();

  if (! rtc.begin()) {
    Serial.println("No RTC"); //Could not fint RTC
    Serial.flush();
    abort();
  }

  /*else {
    DateTime timeInit = rtc.now();
    Serial.print("Today is ");
    Serial.print(timeInit.year(), DEC);
    Serial.print('/');
    Serial.print(timeInit.month(), DEC);
    Serial.print('/');
    Serial.println(timeInit.day(), DEC);
    }*/

  if (! rtc.isrunning()) {
    Serial.println("RTC off, set the time");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.print("Card failed, or not present");
  }
  Serial.println("card initialized.");

  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i / 10 + '0';
    filename[7] = i % 10 + '0';
    if (! SD.exists(filename)) {
      Serial.println("filename exists already");
      logfile = SD.open(filename, FILE_WRITE);
      break;
    }
  }

  if (! logfile) {
    Serial.print("couldnt create file");
  }

  Serial.print("Logging to: ");
  Serial.println(filename);
  Serial.println("DATE,TIME,MILLIS,STATUS,TEMP,HUM");
  logfile.println("DATE,TIME,MILLIS,STATUS,TEMP,HUM");

}

void loop()
{
  static byte currentState = FLUSHING;
  unsigned long currentTime = millis();
  static unsigned long flushStart = currentTime;
  const unsigned long flushPeriod = 1000; //600000 = 10 Minuten
  static unsigned long measureStart;
  const unsigned long measurePeriod = 5000; //300000 = 5 Minuten
  switch (currentState)
  {
    case FLUSHING:

      if (currentTime - flushStart >= flushPeriod)
      {
        DateTime time = rtc.now();
        temp = dht.readTemperature();
        hum = dht.readHumidity();

        Serial.print(time.year(), DEC);
        Serial.print(".");
        Serial.print(time.month(), DEC);
        Serial.print(".");
        Serial.print(time.day(), DEC);
        Serial.print(",");
        Serial.print(time.hour(), DEC);
        Serial.print(":");
        Serial.print(time.minute(), DEC);
        Serial.print(":");
        Serial.print(time.second(), DEC);
        Serial.print(",");
        Serial.print(millis());
        Serial.print(",");
        Serial.print("START_MEASURING");
        measureStart = currentTime;
        currentState = MEASURING;
        Serial.print(",");
        Serial.print(temp);
        Serial.print(",");
        Serial.println(hum);

        if (logfile) {

          logfile.print(time.year(), DEC);
          logfile.print(time.month(), DEC);
          logfile.print(time.day(), DEC);
          logfile.print(",");
          logfile.print(time.hour(), DEC);
          logfile.print(":");
          logfile.print(time.minute(), DEC);
          logfile.print(":");
          logfile.print(time.second(), DEC);
          logfile.print(",");
          logfile.print(millis());
          logfile.print(",");
          logfile.print("START_MEASURING");
          measureStart = currentTime;
          currentState = MEASURING;
          logfile.print(",");
          logfile.print(temp);
          logfile.print(",");
          logfile.println(hum);
          logfile.flush();
        }
        else {

          Serial.println("error opening datalog.txt");

        }
      }
      break;

    case MEASURING:

      if (currentTime - measureStart >= measurePeriod)
      {
        DateTime time = rtc.now();
        temp = dht.readTemperature();
        hum = dht.readHumidity();

        Serial.print(time.year(), DEC);
        Serial.print(time.month(), DEC);
        Serial.print(time.day(), DEC);
        Serial.print(",");
        Serial.print(time.hour(), DEC);
        Serial.print(":");
        Serial.print(time.minute(), DEC);
        Serial.print(":");
        Serial.print(time.second(), DEC);
        Serial.print(",");
        Serial.print(millis());
        Serial.print(",");
        Serial.print("START_FLUSHING");
        flushStart = currentTime;
        currentState = FLUSHING;
        Serial.print(",");
        Serial.print(temp);
        Serial.print(",");
        Serial.println(hum);

        if (logfile) {

          logfile.print(time.year(), DEC);
          logfile.print(time.month(), DEC);
          logfile.print(time.day(), DEC);
          logfile.print(",");
          logfile.print(time.hour(), DEC);
          logfile.print(":");
          logfile.print(time.minute(), DEC);
          logfile.print(":");
          logfile.print(time.second(), DEC);
          logfile.print(",");
          logfile.print(millis());
          logfile.print(",");
          logfile.print("START_MEASURING");
          measureStart = currentTime;
          currentState = MEASURING;
          logfile.print(",");
          logfile.print(temp);
          logfile.print(",");
          logfile.println(hum);
          logfile.flush();
        }
        else {

          Serial.println("error opening datalog.txt");
        }
      }
      break;
  }
}

Thanks to all of you!

Opening the Serial Monitor restarts the sketch.

Try opening the Serial Monitor and then uploading your sketch.

Not on all Arduinos. And seeing while(!Serial) in the sketch, OP might be using one that does not restart the sketch.

There is a "Reset Enable" trace on AVR based boards that reset the boards when you connect to them using the Serial Monitor or any Terminal program. This feature is also used by the programmer when uploading a new sketch. If you don't want this behavior, you can cut the trace and install a microswitch between the two pads. In the closed position, it is connected and the board resets but you can install a new sketch. In the open position, you can open Serial Monitor any time and your program just keeps doing whatever it was doing, but you won't be able to upload new software.

I have done this on several projects and usually label the switch as "Run" and "Program" so I know where it needs to be to upload new code, but switch it back to "Run" so I can connect/disconnect without disturbing execution.