# How to replace a variable lenght record on a SD card

**URL:** <https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086>\
**Category:** Programming\
**Created:** [November 20, 2021, 7:32am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086 "2021-11-20T07:32:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 20, 2021, 7:32am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/1 "2021-11-20T07:32:58Z")

</div>

Hello,

I have "myFile.txt" on a Sd card which holds just one value "Good evening Mark".  
After reading this record I need to rewrite/replace it with "Welcome John".  
Is there a better way to do this rather than removing the .txt file and re creating it ?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 20, 2021, 8:06am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/2 "2021-11-20T08:06:36Z")

</div>

If there is a known maximum number of characters to be stored then you could save the data in fixed length records of that size thus allowing them to be overwritten without re-writing the whole file

This wastes space, but unless the number of records is huge it is not likely to be a problem

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 20, 2021, 8:54am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/3 "2021-11-20T08:54:17Z")

</div>

> [@ba47](#):
>
> Is there a better way to do this rather than removing the .txt file and re creating it ?

you can just overwrite if the new content has the same size or is longer than the previous content.

the [SdFat library](https://github.com/greiman/SdFat) supports the `truncate()` method but the standard builtin SD library does not and you would have to delete and recreate

with the SD library you could explore what you can do with the _O\_TRUNC_ mode

> - O\_TRUNC - If the file exists and is a regular file, and the file is successfully opened and is not read only, its length shall be truncated to 0.

as a way to just replace the content that was there (if that's the intent)

Another approach is to have some data formatting rules, for example you could say that the meaningful data is on the first line - up to the CR/LF mark for example. This way when you overwrite, you insert the end marker and whatever is still in the file after the mark is just to be ignored. (but if security is important you don't want that as previous data might still be left behind).

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 20, 2021, 8:59am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/4 "2021-11-20T08:59:00Z")

</div>

> [@J-M-L](#):
>
> you can just overwrite if the new content has the same size or is longer than the previous content.

I know what you meant, but it is not what you wrote 😀

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 20, 2021, 9:00am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/5 "2021-11-20T09:00:27Z")

</div>

lost in translation again ? 🙂

what I meant is : open the file for writing, seek to position 0, write the new text. close the file

what did I write?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 20, 2021, 9:08am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/6 "2021-11-20T09:08:23Z")

</div>

@ba47 wants to replace a single record in a file for variable length records so cannot just write a longer record in the place of a smaller one, even assuming that he/she knows where to seek() to

I assumed that you meant  
you can just overwrite if the new content has the same size or is **shorter** than the previous content.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 20, 2021, 9:09am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/7 "2021-11-20T09:09:54Z")

</div>

Ah, no I really meant longer because @ba47 said

> [@ba47](#):
>
> which holds just one value

so my understanding was that there was no (meaningful) data after that text in the file.

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 20, 2021, 9:11am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/8 "2021-11-20T09:11:41Z")

</div>

Thank you both.  
I DO NOT have fixed length data and don't know how to use the suggested CR/LF. Do you think the easy way (which I know how to do) i.e. removing and recreating the .txt file will create problems ?

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 20, 2021, 9:24am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/9 "2021-11-20T09:24:50Z")

</div>

I would suggest to move to the SDFat library. That library is overall better than the builtin SD library and then just truncate after writing the new content.

Writing CR/LF is just done by using println() to write your text. That will add the two end markers. Reading the content then could be done by writing a readLine function or just read whatever is in the file and trim after the CR/LF

There is no real harm in deleting the file and recreating one, besides the _wear and tear_ for your SD card - but that happens to some extent as well when you replace the content anyway. if you don't do this 10 times per second every second of the day, that should be no problem

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 20, 2021, 9:38am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/10 "2021-11-20T09:38:36Z")

</div>

Thank you.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 20, 2021, 9:51am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/11 "2021-11-20T09:51:19Z")

</div>

@J-M-L

> [@ba47](#):
>
> which holds just one value

Sorry, I missed that

If the file only contains a single record then I see little point in using an SD card in the first place. You might just as well use EEPROM if you want the sketch to have access to persistent data.

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 21, 2021, 10:54am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/12 "2021-11-21T10:54:01Z")

</div>

Doesn't EEPROM wear out after a while ?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 21, 2021, 11:00am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/13 "2021-11-21T11:00:57Z")

</div>

Each EEPROM location has a nominal life of 100,000 write/erase cycles. How often will you be writing a new record ?

You can also extend the life of the EEPROM by not always writing to the same locations

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 21, 2021, 11:20am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/14 "2021-11-21T11:20:53Z")

</div>

Thanks, 100,000 is good enough. Your suggestion saves extra modules and wiring.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 21, 2021, 11:25am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/15 "2021-11-21T11:25:18Z")

</div>

If you use EEPROM.put() to save the data then only EEPROM cells where the data is changing will actually be written, which may also help prolong the life of the EEPROM with no extra effort

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 21, 2021, 11:38am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/16 "2021-11-21T11:38:06Z")

</div>

You beated me to it. I didn't want to bother you asking how to write/read EEPROM. Never have used it, so I'll study. Will bother you in case I get stuck.  
Thanks again.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 21, 2021, 11:43am UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/17 "2021-11-21T11:43:23Z")

</div>

There are examples in the IDE

Basically you put the string (not a String) of characters to be saved in a zero terminated array of chars then use the EEPROM.put() function to save it. To read it back you declare an array of chars large enough to hold the string then use the EEPROM.get() function to populate the string

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [November 21, 2021, 12:26pm UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/18 "2021-11-21T12:26:56Z")

</div>

dealing with an SD card, you would know if the file is missing before reading the data.  
In case of EEPROM, you just have bytes available but don't know if it's relevant data or garbage (the first time you try to read, the EEPROM has not been initialized and thus the get() will return garbage.

A way to deal with this is to add a magic keyword (say an unsigned long) at a known address in EEPROM. When you want to read the string, you first perform a get() at the magic keyword address and if you find the expected data then you can assume the string is correctly stored at the next address. if you don't find the keyword, then you need to have some error handling mechanism, possibly providing a default value (and writing it to the EEPROM with the keyword for next time).

---

<div class="post-metadata">

**Author:** ![ba47](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ba47/32/1100561_2.png) [@ba47](https://forum.arduino.cc/u/ba47)\
**Post date:** [November 21, 2021, 2:50pm UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/19 "2021-11-21T14:50:53Z")

</div>

Ok, thanks.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 20, 2022, 2:51pm UTC](https://forum.arduino.cc/t/how-to-replace-a-variable-lenght-record-on-a-sd-card/927086/20 "2022-05-20T14:51:07Z")

</div>

Closed on May 20, 2022, 2:51pm UTC

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