Reading decimal data from SD, and turn it into floats

Greetings,

I've been trying to accomplish something in the Arduino IDE for a few hours and can't figure this one out. I have a text file in the SD card that stores the last recorded GPS coordinates in the format of 11.111111,-22.222222 , I then want to read that data off of the card later for other use.

I've not been able to successfully do this at all, and the closest thing I've came to is the code below I copied and slightly modified from another post. And the last attempt gave me this output, just missing all the decimals:

x: 11.000000
y: -22.000000

I don't know enough about these sort of char/int/float manipulations that's seen in the code, so I really need help figuring it out...

#include "FS.h"
#include "SD.h"
#include "SPI.h"

File root;

bool readLine(File &f, char* line, size_t maxLen) {
  for (size_t n = 0; n < maxLen; n++) {
    int c = f.read();
    if ( c < 0 && n == 0) return false;  // EOF
    if (c < 0 || c == '\n') {
      line[n] = 0;
      return true;
    }
    line[n] = c;
  }
  return false; // line too long
}

bool readVals(float* v1, float* v2) {
  char line[40], *ptr, *str;
  if (!readLine(root, line, sizeof(line))) {
    return false;  // EOF or too long
  }
  *v1 = strtol(line, &ptr, 10);
  if (ptr == line) return false;  // bad number if equal
  while (*ptr) {
    if (*ptr++ == ',') break;
  }
  *v2 = strtol(ptr, &str, 10);
  return str != ptr;  // true if number found
}

void setup() {
  Serial.begin(115200);
  if (!SD.begin(4)) {   // SD.begin(CS pin)
    Serial.println("Card Mount Failed");
    return;
  }

  float x, y;
  root = SD.open("/LASTGPS.Txt", FILE_READ);
  if (!root) {
    Serial.println("open error");
    return;
  }
  while (readVals(&x, &y)) {
    Serial.print("x: ");
    Serial.println(x, 6);
    Serial.print("y: ");
    Serial.println(y, 6);
    Serial.println();
  }
  Serial.println("Done");
}

void loop() {
}

Thanks in advance!

Hi,
strtol() converts a string to a long integer, you want strtod()
i.e.

//Instead of:
*v1 = strtol(line, &ptr, 10);

//use
*v1 = strtod(line, &ptr);

Yours,
TonyWilk

TonyWilk:
Hi,
strtol() converts a string to a long integer, you want strtod()
i.e.

//Instead of:

*v1 = strtol(line, &ptr, 10);

//use
*v1 = strtod(line, &ptr);




Yours,
TonyWilk

Thanks for the quick reply, I didn't realize the base 10 part at the end also needed to go! All working great now!