(Solved) Overwriting a line in a txt file on an SD card

Hi all,

Thanks again for readings these.

Goal: to overwrite a line in a txt SD file.

Code:

#include <SD.h>
File myFile;

void setup () {
  pinMode (53, OUTPUT);
  SD.begin (53);
  Serial.begin (9600);
  
  myFile = SD.open ("testing.txt", FILE_WRITE);
  for (int i = 0; i <= 9; i++) {
    myFile.print ("line: ");
    myFile.println (i);
  }
  myFile.close ();

  myFile = SD.open ("testing.txt");
  myFile.seek (0);
  String someBuffer = myFile.readStringUntil ('\n');
  int targetPosition = myFile.position () + 1;
  myFile.close ();

  myFile = SD.open ("testing.txt", FILE_WRITE);
  myFile.seek (targetPosition);
  myFile.println ("new text added");
  myFile.close ();
  








  
}

void loop () {
  
}

Issue: the line is printed not at the desired location, but at the "end" of the file.

Please let me know if I can provide anything else. Thank you!

if you print targetPosition, what do you see ?

Thanks for the reply.

It doesn't print there; it prints at the "end" of the txt file (aka after the last line).

--> what's the value of that variable ? (and the size of the file). Run this

#include <SD.h>
File myFile;

void setup () {
  pinMode (53, OUTPUT);
  SD.begin (53);
  Serial.begin (9600);
  
  myFile = SD.open ("testing.txt", FILE_WRITE);
  for (int i = 0; i <= 9; i++) {
    myFile.print ("line: ");
    myFile.println (i);
  }
  myFile.close ();

  myFile = SD.open ("testing.txt");
  myFile.seek (0);
  String someBuffer = myFile.readStringUntil ('\n');
  int targetPosition = myFile.position () + 1;

  Serial.print("targetPosition = ");
  Serial.println(targetPosition);

  Serial.print("file size = ");
  Serial.println(myFile.size());

  myFile.close ();

  myFile = SD.open ("testing.txt", FILE_WRITE);
  myFile.seek (targetPosition);
  myFile.println ("new text added");
  myFile.close ();
   
}

void loop () {}

you could also print someBuffer to make sure this is what you expected.

Got it. Will do tomorrow once I’m back in the office.

Issue: the line is printed not at the desired location, but at the "end" of the file.

SD.h is said to be "a slightly more friendly wrapper for sdfatlib". In this case the friendly dog has bitten you.

SD.h contains the following

#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)

The O_APPEND forces the write to the end of the file, even though you use file.seek() and try to write at a location.

Use O_RDWR as the mode

//myFile = SD.open ("testing.txt", FILE_WRITE);
  myFile = SD.open ("testing.txt", O_RDWR);
  myFile.seek (targetPosition);
  myFile.println ("new text added");
  myFile.close ();

I think that if you begin to write at the targetPosition and the text you are adding is longer than the text you are replacing you will overwrite into the next line.

cattledog:
SD.h is said to be "a slightly more friendly wrapper for sdfatlib". In this case the friendly dog has bitten you.

SD.h contains the following

#define FILE_WRITE (O_READ | O_WRITE | O_CREAT | O_APPEND)

The O_APPEND forces the write to the end of the file, even though you use file.seek() and try to write at a location.

Use O_RDWR as the mode

//myFile = SD.open ("testing.txt", FILE_WRITE);

myFile = SD.open ("testing.txt", O_RDWR);
  myFile.seek (targetPosition);
  myFile.println ("new text added");
  myFile.close ();




I think that if you begin to write at the targetPosition and the text you are adding is longer than the text you are replacing you will overwrite into the next line.

Wow. It works perfectly. Thank you.

Good thing the dog wasn't rabid (or was it...)!