Program prints weird numbers to sd card

I was creating a sketch to run a custom-formatted program from a sd card. I am working on creating, changing and reading variables but when I created the creating function, it printed weird numbers to the sd card. It printed 989711097110974449501310 for some reason. Here is the function:

void mkVar(String varn, String val) {
  varFile.seek(varFile.size());
  varFile.print(varn);
  varFile.print(",");
  varFile.println(val);
  Serial.print("p"); // Debugging purposes
}

The input was banana,12

Does your object allow for "write" rather than "print?" For example:

varFile.write(varn); // testing ".write"

@xfpd the write() function does not support Strings

I just saw this... a "decimal to ASCII converter" (if you separate the "letters" correctly) showed "ba...12"

CoderStool.com

Here it is:

98 97 110 97 110 97 44 49 50 13 10 
banana,12<CR><LF>

So... you need a way to "write" your String.

Thanks, my read function does say 12 when inputting banana. I still need to fix my changing value function

void setVar(String varn, String val) {
  varFile.seek(0);
  String d = "";
  while (varFile.available()) {
    d += varFile.read();
  }
  Serial.print("(" + d + ")");
  varFile.close();
  SD.remove("/sys/ram.txt");
  varFile = SD.open("/sys/ram.txt", FILE_WRITE);
  d.replace(varn + "," + getVar(varn), varn + "," + val);
  varFile.println(d);
  varFile.close();
}

When running the read function before changing, it is 12 but after changing it is blank

Try the dump in hex so you do not need to deal with 3-digit decimal:

varFile.print(varn, HEX);

Can you show another output showing this?

12<(989711097110974449501310)><
the stuff inside >< is the getVar data. the stuff inside the () is the what the sd card's contents are

That is still "banana,12"

after the brackets, it runs the setVar() function to change the value. that function does not work so the next reading (the >< after the brackets) are blank

Could this be because varFile.read() returns an int, so d += varFile.read(); will append the hexadecimal decimal representation to the string d? Try doing d += char(varFile.read()); instead to see if that fixes it.

It will append the number in "normal" decimal notation, hence 989711097110974449501310.

The reason read returns a signed int instead of a char (or uint8_t) is for when there is nothing available: it returns -1. So the loop can more obviously express this behavior like

int c;
while ((c = varFile.read()) >= 0) {
  d += (char) c;
}

(although that does leave the variable c in scope after the loop)

Calling read() until it returns -1 is my preferred method too. I've long thought that calling available() before read() is redundant since read() already tells you if anything was available.

I used char(varFile.read()); and it read it normal (it said banana,12 instead of 989711097110974449501310) thanks. The setVar() function still doesn't work sadly. I'll keep you updated

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.