Random access to SD card using SD.h or another library (ESP32)

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?

Thanks in advance!

Solved!

The solution was to migrate from the SD library to mySD, which seems to be a SdFat wrapper for ESP32.

You can see in the file mySD.h that the FILE_WRITE mode is defined as:

#define FILE_WRITE (F_READ | F_WRITE | F_CREAT)

Which means that it allows random access to the file for writing (F_WRITE instead of FILE_APPEND).

PS: I want to remark @Juraj comment since the forum is mainly for Arduino board questions

the SD library in ESP32 boards support package is different than the standard Arduino SD library

Welcome.
Yes, you are using the Arduino core which gives the ESP the appearance of being Arduino compatible from an IDE prospective.

Also, Espressif runs a separate Arduino forum:
https://esp32.com/viewforum.php?f=19

FYI: ESP32/ESP8266 are represented by different site ownership. ESP8266 is here:

https://www.esp8266.com/viewforum.php?f=25

Have fun,

Ray

1 Like

@mrburnette those forums are not Arduino specific and the question is about ESP32 Arduino and Arduino SD library (the ESP32 core implementation)

You are mistaken:

image

image

And, for your future reference, igrr over on github.com is a frequent contributor/author of the two cores:

image

image

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.

Enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field

Thus, and I sumarize, there are other Espressif forum resources for both the ESP32 and the ESP8266 that are Arduino-centric.

Ray

esp arduino users are welcome on Arduino forum. it is not a forum only for boards by Arduino.

btw: 5 Arduino models have esp32 on board. except of those arduinos only esp8266 and esp32 are supported by Arduino on Arduino IoT cloud.

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.

  1. User was welcomed
  2. Yes. My response to Op's "... is proper ..."
  3. 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.

Ray

@mrburnette ok sorry

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