Writing different data in 3 files on SD-card results in all data in last file

Hi,

I want to write three String in three different files. But the result of my code is, that everything gets written in the last opened file. Does someone see where my mistake is?

This is my code:

unsigned long lastPositionT = 0;
unsigned long lastPositionH = 0;
unsigned long lastPositionP = 0;

File tempDataFile;
File humDataFile;
File pressDataFile;

void logToSdCard() {

  Serial.println("Log to SD-Card...");

 String str_filename = "t_"+getMonthYearString()+".txt";
 char* temp_filename = (char*) str_filename.c_str();

 str_filename = "h_"+getMonthYearString()+".txt";
 char* hum_filename = (char*) str_filename.c_str();

 str_filename = "p_"+getMonthYearString()+".txt";
 char* press_filename = (char*) str_filename.c_str();
 
 Serial.print("Current files to log to: ");
 Serial.println(temp_filename);
 Serial.println(hum_filename);
 Serial.println(press_filename);

 temperature = bme.readTemperature();
 humidity = bme.readHumidity();
 pressure = bme.readPressure() / 100.0F;

 char temp_timestamp[50];
 timeClient.update();
 unsigned long rawTimestamp = timeClient.getEpochTime();
 Serial.println(rawTimestamp);
 ltoa(rawTimestamp, temp_timestamp, 10); 

 SD.begin(16);
 
 tempDataFile = SD.open(temp_filename, FILE_WRITE);
 
 if (tempDataFile) {
   tempDataFile.print("[");
    tempDataFile.print(temp_timestamp);
    tempDataFile.print("000,");
    tempDataFile.print(temperature);
    tempDataFile.println("]");
   lastPositionT = tempDataFile.position();
   tempDataFile.flush();
   tempDataFile.close();

   Serial.print("Timestamp: ");
   Serial.println(temp_timestamp);
   Serial.print("Temp: ");
   Serial.println(temperature);
   Serial.println("Data was successfully saved on the sd-card!");
   
 } else {
   Serial.println("error opening temp-text-File!");
 }

 SD.end();

 SD.begin(16);
 
 humDataFile = SD.open(hum_filename, FILE_WRITE);
 
 if (humDataFile) {
   humDataFile.print("[");
    humDataFile.print(temp_timestamp);
    humDataFile.print("000,");
    humDataFile.print(humidity);
    humDataFile.println("]");
   lastPositionH = humDataFile.position();
   humDataFile.flush();
   humDataFile.close();
 
   Serial.print("Timestamp: ");
   Serial.println(temp_timestamp);
   Serial.print("Humidity: ");
   Serial.println(humidity);
   Serial.println("Data was successfully saved on the sd-card!");
   
 } else {
   Serial.println("error opening temp-text-File!");
 }

 SD.end();

 SD.begin(16);

  pressDataFile = SD.open(press_filename, FILE_WRITE);

 if (pressDataFile) {
   pressDataFile.print("[");
    pressDataFile.print(temp_timestamp);
    pressDataFile.print("000,");
    pressDataFile.print(pressure);
    pressDataFile.println("]");
   lastPositionP = pressDataFile.position();
   pressDataFile.flush();
   pressDataFile.close();

   Serial.print("Timestamp: ");
   Serial.println(temp_timestamp);
   Serial.print("Pressure: ");
   Serial.println(pressure);
   Serial.println("Data was successfully saved on the sd-card!");
   
 } else {
   Serial.println("error opening temp-text-File!");
 }
 
 SD.end();
}

User5518:
everything gets written in the last opened file…
This is my code:

I’m betting it isn’t.
This is an Arduino forum. If you are using an Arduino, you might consider deleting it all and starting over with the Datalogging example included in the Arduino IDE. After you have seriously considered whether you really need three separate files, and concluded that you do, it should be just a matter of opening, recording, and closing them in sequence.

Note also the stickies and how-tos at the head of the forums, including those on posting code in code tags </>.

Post or attach all your code - snippets aren't going to cut it.

Use of Strings is risky - the SD class is allocating a large buffer when you open a file so your Strings have the possibility of causing fragmentation leading to undesired behaviour.

I "sloved" it by writing everything in one file and separate it while reading it. Thanks for the informations. I want to avoid using strings in future projects and will inform myself about alternatives.