user78081: 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!

user78081: 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


78081: <p>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.</p>
<p>The issue is it seems that the SD.h library has only 3 open modes (Read only, <code>FILE_WRITE</code>, <code>FILE_APPEND</code>), and I cannot figure out how to use random access for writing a specific byte of the file.</p>
<p>Imagine the situation: I have a file called <code>myFile.txt</code> which initially has a size of 5 bytes, and its content is:
<code>abcde</code></p>
<p>The code I'm using is the following:</p>
<pre class="lang-cpp prettyprint-override"><code>#include &lt;SPI.h&gt;
#include &lt;SD.h&gt;

#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(&quot;Initializing SD card...&quot;);

  if (!SD.begin(SD_CS_PIN)) {
    Serial.println(&quot;initialization failed!&quot;);
    return;
  }
  Serial.println(&quot;initialization done.&quot;);



  myFile = SD.open(&quot;/myFile.txt&quot;, FILE_WRITE);
  myFile.seek(3);
  myFile.write('a');
  myFile.close();

}

void loop() {}
</code></pre>
<p>But after running the code, the file has an unexpected behaviour, its size changes from 5 bytes to 4 bytes, and its content is:
<code>abca</code>
instead of the expected
<code>abcae</code></p>
<p>I know it happens because when writing into the file I'm using the open mode <code>FILE_WRITE</code> which uses the <code>FILE_APPEND</code> mode internally, so when doing <code>myFile.seek(3);</code>, I'm truncating the file to 4 bytes since the <code>FILE_APPEND</code> mode needs to point to the end of the file.</p>
<p>So, my question is, how can I open a file in random access mode with SD.h, or another library?</p>
<p>Thanks in advance!</p>
 78081: <p><strong>Solved!</strong></p>
<p>The solution was to migrate from the <code>SD</code> library to <a href="https://github.com/nhatuan84/esp32-micro-sdcard" rel="nofollow noreferrer"><code>mySD</code></a>, which seems to be a <code>SdFat</code> wrapper for ESP32.</p>
<p>You can see in the file <a href="https://github.com/nhatuan84/esp32-micro-sdcard/blob/master/mySD.h#L30" rel="nofollow noreferrer"><code>mySD.h</code></a> that the <code>FILE_WRITE</code> mode is defined as:</p>
<pre class="lang-cpp prettyprint-override"><code>#define FILE_WRITE (F_READ | F_WRITE | F_CREAT)
</code></pre>
<p>Which means that it allows random access to the file for writing (<code>F_WRITE</code> instead of <code>FILE_APPEND</code>).</p>
<p><strong>PS:</strong> I want to remark @Juraj comment since the forum is mainly for Arduino board questions</p>
<blockquote>
<p>the SD library in ESP32 boards support package is different than the standard Arduino SD library</p>
</blockquote>
