Writing float as binary on SD card

Hi All,

I am trying to write float numbers to an SD card in a binary format, but the SD library write() function
only supports byte, char and string formats.

Is there a simple way of converting float numbers to binary? I have tried using unions in the form below:

SdFat sd; //file system object
SdFile dataFile; //log file

void writePoint(); {

union{byte uBytes[4]; float uFloat;} dataPoint;

dataFile.open("datalog.bin", FILE_WRITE);
dataPoint.uFloat = 3.14;
dataFile.write(dataPoint.uBytes, 4);
dataFile.close();

}

For some reason this code keeps crashing. Any suggestions on a solution? Thanks!

Do you actually check to see if the file was opened successfully? Some SD cards are pretty finicky. You probably need to check the return value from open() to make sure.

void writePoint(); {

Why is that ; there?