# Overwriting values on SD card

**URL:** <https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451>\
**Category:** General Guidance\
**Created:** [August 27, 2018, 9:36am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451 "2018-08-27T09:36:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 9:36am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/1 "2018-08-27T09:36:42Z")

</div>

This is quiet an old topic, but I have not been able to find an answer in other threads.

I am using the SD.lib for storing some sensor values on an SD card. Specifically, in this project I want to make several files each storing 72 values all of the same length.

So far, I have succeed in writing to the end of the file and reading specific positions by using position() and seek(). I have tried to use position() and write() to overwrite a specific line of the file, but the write() function will always write to the end of the file.

I have read that instead of using myFile = SD.open("test.txt", FILE\_WRITE) it is possible to open the file in truncate mode, which should make overwriting possible. But, how do I do this?

What I want to do:  
My file has the following content:

12341  
12342  
12343  
12344

And I want to overwrite line 2 with 12345 so the file becomes:

12341  
12345  
12343  
12344

---

<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:** [August 27, 2018, 9:59am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/2 "2018-08-27T09:59:24Z")

</div>

> I have read that instead of using myFile = SD.open("test.txt", FILE\_WRITE) it is possible to open the file in truncate mode, which should make overwriting possible. But, how do I do this?

If you open a file, in truncate mode, that has data in it, that data will be deleted. I do not think that that is what you want to do.

> I have tried to use position() and write() to overwrite a specific line of the file, but the write() function will always write to the end of the file.

If that statement IS true, there is something wrong with your code.

If you want help with your code, you must post your code, in code tags.

---

<div class="post-metadata">

**Author:** ![adwsystems](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adwsystems/32/1126723_2.png) [@adwsystems](https://forum.arduino.cc/u/adwsystems)\
**Post date:** [August 27, 2018, 10:07am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/3 "2018-08-27T10:07:16Z")

</div>

That's funny. That's the same thing I need to do. I haven't been able to figure out an easy or short way to do it. If you figure it out, please post it.

---

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 10:32am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/4 "2018-08-27T10:32:20Z")

</div>

> PaulS:  
> If you open a file, in truncate mode, that has data in it, that data will be deleted. I do not think that that is what you want to do.  
> If that statement IS true, there is something wrong with your code.
> 
> If you want help with your code, you must post your code, in code tags.

Okay, thank you. I was spending a lot of time on this and couldn't figure out, why it kept deleting the content of my file. So it turns out that I did open it in truncate mode, but that truncate mode is not what I want.

Now I opened it again using FILE\_WRITE and was able to overwrite a line as i wanted. I will post an example later in case others have the same problem.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 11:14am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/5 "2018-08-27T11:14:04Z")

</div>

> adwsystems:  
> That's funny. That's the same thing I need to do. I haven't been able to figure out an easy or short way to do it. If you figure it out, please post it.

So I start by the following:

```arduino
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
 Serial.begin(9600);
 
 SD.begin(4);
 myFile = SD.open("test.txt", FILE_WRITE);

 //write to file   
 myFile.println("12341"); //writes line 1
 myFile.println("12342"); //writes line 2
 myFile.println("12343"); //writes line 3
 myFile.println("12344"); //writes line 4
 myFile.close();

 //read from file
 myFile = SD.open("test.txt");

 Serial.println(readLine()); //reads line 1
 Serial.println(readLine()); //reads line 2
 Serial.println(readLine()); //reads line 3
 Serial.println(readLine()); //reads line 4
 myFile.close();  
}

String readLine()
{
 String received = "";
 char ch;
 while (myFile.available())
 {
   ch = myFile.read();
   if (ch == '\n')
   {
     return String(received);
   }
   else
   {
     received += ch;
   }
 }
 return "";
}

void loop() {
 // nothing happens after setup
}

```

This creates a file with the desired content and outputs the content to the serial monitor:  
12341  
12342  
12343  
12344

To find the position of the begining of line 2, I run the following code:

```arduino
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
 Serial.begin(9600);
 
 SD.begin(4);

 //read from file
 myFile = SD.open("test.txt");

 Serial.println(readLine()); //reads line 1
 Serial.println(myFile.position()); //prints the position for the beginning of line 2
 Serial.println(readLine()); //reads line 2
 Serial.println(readLine()); //reads line 3
 Serial.println(readLine()); //reads line 4
 myFile.close();
}

String readLine()
{
 String received = "";
 char ch;
 while (myFile.available())
 {
   ch = myFile.read();
   if (ch == '\n')
   {
     return String(received);
   }
   else
   {
     received += ch;
   }
 }
 return "";
}

void loop() {
 // nothing happens after setup
}

```

The output on the serial monitor is:  
12341  
7  
12342  
12343  
12344

This output shows that line 2 starts at position 7.  
Line 2 can be overwritten with:

```arduino
#include <SPI.h>
#include <SD.h>

File myFile;

void setup() {
 Serial.begin(9600);
 
 SD.begin(4);
 myFile = SD.open("test.txt", FILE_WRITE);

 //write to file
 myFile.seek(7); //goes to the beginning of line 2    
 myFile.println("12345");
 myFile.close();

 //read from file
 myFile = SD.open("test.txt");

 Serial.println(readLine()); //reads line 1
 Serial.println(readLine()); //reads line 2
 Serial.println(readLine()); //reads line 3
 Serial.println(readLine()); //reads line 4
 myFile.close();
}

String readLine()
{
 String received = "";
 char ch;
 while (myFile.available())
 {
   ch = myFile.read();
   if (ch == '\n')
   {
     return String(received);
   }
   else
   {
     received += ch;
   }
 }
 return "";
}

void loop() {
 // nothing happens after setup
}

```

So now its time to write the real program 🙂

---

<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:** [August 27, 2018, 11:31am UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/6 "2018-08-27T11:31:35Z")

</div>

```cpp
 String received = "";
 char ch;
 while (myFile.available())
 {
  ch = myFile.read();
  if (ch == '\n')
  {
    return String(received);

```

Why do you create a new String instance from the received String?

---

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 1:24pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/7 "2018-08-27T13:24:54Z")

</div>

> PaulS:
> 
> ```arduino
> String received = "";
> 
> ```

char ch;  
while (myFile.available())  
{  
&nbsp; ch = myFile.read();  
&nbsp; if (ch == '\n')  
&nbsp; {  
&nbsp; &nbsp; return String(received);

> ```arduino
> 
> Why do you create a new String instance from the received String?
> 
> ```

It is some code, that I copied form [Arduino SD Card Tutorial - educ8s.tv - Watch Learn Build](http://educ8s.tv/arduino-sd-card-tutorial/). I think the point is, that it only reads characters until the line terminates and not everything that is stored in the file.

Or maybe I don't understand your question. I'm pretty new to programming.

---

<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:** [August 27, 2018, 1:27pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/8 "2018-08-27T13:27:16Z")

</div>

> It is some crap, that I copied form [Arduino SD Card Tutorial - educ8s.tv - Watch Learn Build](http://educ8s.tv/arduino-sd-card-tutorial/).

Oh, well.

---

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 1:38pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/9 "2018-08-27T13:38:14Z")

</div>

> PaulS:  
> Oh, well.

Ha ha 🙂 Can you enlighten me why it is crap? Is it because, he can save memory by reusing the other string?

Anyway, I am not going to use the code, since I want to read numbers and store them in an int. So I wrote this function (numbers stored on the SD card always consist of 5 characters):

```arduino
int readNumber()
{
  int x = 0;
  int z = 10000;
  
  for (int i=0; i<5; i++){
    x = (myFile.read()-48)*z + x;
    z = z / 10; 
  }

  return x;
}

```

Please feel free to comment for improvements 🙂

---

<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:** [August 27, 2018, 1:47pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/10 "2018-08-27T13:47:20Z")

</div>

> Is it because, he can save memory by reusing the other string?

Partly it is crap because there is no reason to make a copy of the String.

```arduino
  return received;

```

Partly it is crap, because it uses Strings in the first place. With the limited memory on the Arduino, the String class, with all the dynamic memory allocation/freeing that it does, is just not a good fit.

```arduino
    x = (myFile.read()-48)*z + x;

```

Where did the 48 come from? While I know the answer,

```arduino
    x = (myFile.read() - '0')*z + x;

```

does not require that I know anything about the ASCII table.

---

<div class="post-metadata">

**Author:** ![rmose](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@rmose](https://forum.arduino.cc/u/rmose)\
**Post date:** [August 27, 2018, 1:51pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/11 "2018-08-27T13:51:30Z")

</div>

> Where did the 48 come from? While I know the answer,
> 
> ```arduino
> x = (myFile.read() - '0')*z + x;
> 
> ```

Ah, cool. Thanks for all the help

---

<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 5, 2021, 6:39pm UTC](https://forum.arduino.cc/t/overwriting-values-on-sd-card/543451/12 "2021-05-05T18:39:44Z")

</div>


