Concatenate different data-types

Hi, I have a GPS system that is installed in a vehicle. That means it can be turned off without warning.

I am sometimes getting garbage written to an SD card file, my guess is it was shut down before closing the file.

I am trying to keep the time between opening the file and closing it as short as possible. Is there some way to make an entire "string" to print before opening then do it in one action?

The caveat is that I am desperately low on Flash (409-Bytes) so need to make any concatenating as memory-lean as possible. I am thinking that if I can have one complete string, "03/29/2017,12:24:33,49.12345,-149.12345,..." then an Open, a single Print-line and Close, might lessen the risk at engine shut downs..

        gpsFile = SD.open(fname,FILE_WRITE);
        if (gpsFile) {
          sprintf(gpsDT,"%02d/%02d/%02d",gpsMonth,gpsDay,gpsYear);
          gpsFile.print(gpsDT);
          gpsFile.print(F(","));
          sprintf(gpsDT,"%02d:%02d:%02d",gpsHour,gpsMinute,gpsSecond);
          gpsFile.print(gpsDT);
          gpsFile.print(F(","));
          gpsFile.print(gps.location.lat(),10);
          gpsFile.print(F(","));
          gpsFile.print(gps.location.lng(),10);
          gpsFile.print(F(","));
          gpsFile.print(gps.speed.mph(),2);
          gpsFile.print(F(","));
          gpsFile.print(gpsCourse());
          gpsFile.print(F(","));
          gpsFile.print(gps.altitude.feet(),2);
          gpsFile.print(F(","));
          gpsFile.print(gps.satellites.value());
          gpsFile.println();
          gpsFile.close();
        } else {
          Serial.println(F("Failed to open file"));
        }  // gpsFile

I found this, but not sure if it helps or not. http://forum.arduino.cc/index.php?topic=96299.0

I am trying to keep the time between opening the file and closing it as short as possible. Is there some way to make an entire "string" to print before opening then do it in one action?

There is, but it wouldn't help. Data written to the file isn't really written to the file. It is written to a buffer. Only when the buffer is full is it written to the file. You can force the buffer to be written to the file, by using the flush() method of the File class.

You could use a super capacitor to provide backup power (or a battery), and monitor the engine voltage. Close the file when the engine voltage drops, before the capacitor or battery runs out.

PaulS: You can force the buffer to be written to the file, by using the flush() method

Thanks Paul, I will use the Flush() method as the super-cap voltage monitoring electronics is well out of my skill level with a soldering iron. The buffering before the Write would explain the garbage I sometimes get. It does not happen very often so will live with that.

Something to possibly lessen the frustration of others, while searching, I discovered that fsprint is very limited in Ubuntu/Linux.

The specifiers, "%f" and "%c" do not work. That drove me nuts for a while getting "?" in the results instead of floats. I stumbled onto a note way down at the bottom of a very long arduino.cc page on specifiers. DUH!

Your help is very much appreciated.

So you need a 5v UPS with at least few seconds capacity, which sends a signal before shutting down.

Google 5v UPS, or take this question to one of the electronics subs on this board.

Hmm - I have started a thread: http://forum.arduino.cc/index.php?topic=466970