On an ESP32 I am saving the integer value 1008 with the following function:
void writeIntFile(fs::FS &fs, const char *path, int message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for writing");
return;
}
if (file.print(message)) {
Serial.println("File written");
} else {
Serial.println("Write failed");
}
file.close();
}
When I try and retrieve the integer via the following function, the integer 49 is returned:
int readIntFile(fs::FS &fs, const char *path) {
Serial.printf("Reading file: %s\n", path);
File file = fs.open(path);
if (!file) {
Serial.println("Failed to open file for reading");
// return x;
}
Serial.print("Read from file: ");
while (file.available()) {
int x = file.read();
return x;
}
file.close();
}
I am new to saving data on an SD in the arduino environment, what am I doing wrong?
try to explain to yourself line by line what this does
do you think you are building an int (4 bytes on your ESP32)? do you think the while has any chance to run twice? do you think you are closing the file?
you need to decide how you store the data in the file and also think about separators if you save the value in ASCII (what print will do).
say you want to save 1234 and 5678 in the file.
first print will get you in the file
1234
and then after second print the file will hold
12345678
➜ now when you try to read 2 numbers you don't know where to separate them. was it 12 and 345678 or 123 and 45678...
➜ you need an end marker at the minimum to separate your numbers, println() will add a CR and LF and your file will look like this
Like I said I am learning how to use the SD card in an Arduino environment. I have attempted to adapt the functions in the SD card example, which were text string based, to accept and return an integer. The adaption works insofar as it compiles and does return an integer but there is obviously something wrong.
I cannot answer the questions you put to me yet, I am in the process of learning.
I do not want to save any string or array, I want to save an integer which will be around 1010 decimal and compare it with a similar value integer. I have written a sketch which uses Open Weather Maps to download and display weather information and want to save the air pressure reading so I can compare old and new readings. To save battery I shutdown my device and start it up every half hour so I need the SD card or EEPROM to keep the air pressure data during shutdown.
has dumped the number into the file. the file looks like this
1008
so when you do the
you have various commands:
while (file.available()) {
this tests if something is available in the file you just opened. Since you have just written something in there, the answer is true and you enter the while loop
int x = file.read();
read() retrieves only one bye and you store that into x. What you read is the first character in the file which is the letter '1' (a character) encoded in ASCII so the value 49 is saved into x
then you do return x;
so the functions terminates there and you return 49
the while does not get a chance to loop and terminates when the file has all been read and the close() is never called.
so that's why it does not work
reading from a file (or from a stream in general) requires knowledge of what you expect and is often conducted byte by byte. I would suggest to study Serial Input Basics to see how this is done with the Serial port. Handling a file is the same, you read from the File instead of Serial.
Thank you. This will help me enormously on my journey.
But if there is no simple way of saving an integer and retrieving it ready for mathematical comparison I might as well save it as a string and use .toInt() to convert it back to an integer. It's just that converting from integer to string to save and then converting back from string to integer seems a waste of resources to me.