Problem with sprintf and sscanf for floating point numbers

I am trying to write and read floating point numbers from an SD card.

I have succeeded to write and recover integers by using

    sprintf(tString,"%d,%d,"i,j);

and
    sscanf(tString,"%d,%d",&c,&d);

However, when I use the %f token, the floating point number is “printed” as ?

The sscanf always returns 0.00 for the floating point number, even when I hard code the numbers by

   sprintf(tString,"2.35,-47.456");

Any ideas?

Thanks,

Gareth

Here is the complete code

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */

#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {

  char fName[63];
  char tString[32];
  char temp[3];
  int a;
  float c,d;
  //char b[10];
  int  i = 1;
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  strcpy(fName,"another/test.txt");
  
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  while (SD.exists(fName))
  {
    Serial.println("File already exists");
    strcpy(fName,"another/test");
    itoa(i++,temp,10);
    strcat(fName,temp);
    strcat(fName,".txt");
    Serial.println(fName);
  }
 
  myFile = SD.open(fName, FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to ");
    Serial.print(fName);
    sprintf(tString,"%f,-45.756,",2.35);
    Serial.println(tString);
    myFile.print(tString);
    myFile.print("\n");
    // close the file:
    myFile.close();
    Serial.println("...done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open(fName);
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      a = (myFile.read());
      Serial.print(char(a));
      if (a == char(10))
        strcat(tString,'\0');
      else
        strcat(tString,a);
    }
    Serial.println();
    Serial.println(tString);
    sscanf(tString,"%f,%f",&c,&d);
    Serial.println(c);
    Serial.println(d);
    
    
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

Floating point in printf() is disabled by default because of the space it requires. It is possible to enable it, and the info is posted elsewhere.

We recommend to use the better alternatives, dtostrf() for printing and atof() for input.

Thanks, at least it is not just me!

I guess I'll just need to work a bit harder and code my own reading and writing of numbers to strings and then save and recover those from the SD!

If you are just storing sensor data, floating point is not necessary and usually not even desired.