Hello everyone, I know the forum is for arduino so I don't know if it's properly to ask an ESP32 question here. I'm trying to use a SD card with my ESP32 in order to save some variables in a txt file. Each variable uses 1 byte, so they can be represented by an 8 bit extended ASCII character.
The issue is it seems that the SD.h library has only 3 open modes (Read only, FILE_WRITE, FILE_APPEND), and I cannot figure out how to use random access for writing a specific byte of the file.
Imagine the situation: I have a file called myFile.txt which initially has a size of 5 bytes, and its content is: abcde
The code I'm using is the following:
#include <SPI.h>
#include <SD.h>
#define SD_CS_PIN 5
File myFile;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Initializing SD card...");
if (!SD.begin(SD_CS_PIN)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
myFile = SD.open("/myFile.txt", FILE_WRITE);
myFile.seek(3);
myFile.write('a');
myFile.close();
}
void loop() {}
But after running the code, the file has an unexpected behaviour, its size changes from 5 bytes to 4 bytes, and its content is: abca instead of the expected abcae
I know it happens because when writing into the file I'm using the open mode FILE_WRITE which uses the FILE_APPEND mode internally, so when doing myFile.seek(3);, I'm truncating the file to 4 bytes since the FILE_APPEND mode needs to point to the end of the file.
So, my question is, how can I open a file in random access mode with SD.h or another library?
And, for your future reference, igrr over on github.com is a frequent contributor/author of the two cores:
And while not an authority, he is much read and referenced in this forum, Rui Santos from randomnerdtutorials.com, even explains that the ESP32 core is a 3rd-party product.
I'm at a loss to what the "issue is here" ... my only intent was to provide additional links for resources from the web, no different than posting a link to a Wikipedia article.
User was welcomed
Yes. My response to Op's "... is proper ..."
Then, I continued with also
IF you think I was dissing the Op, then you have definitely not read enough of my responses; when I diss, there is no doubt.