Overwrite SD File content?

I am trying to overwrite an SD log file's entries from the beginning of the file, as a way to "delete" them after they are successfully uploaded to the cloud. This is a workaround as there isn't a way to delete specific file content, only to add to it or overwrite it (as I understand it).

After a couple of days of experimentation and debug, I am unable to overwrite any file content. I do this:
datafile.open("Logfile", FILE_WRITE);
datafile.seek(58); // beginning of file content to overwrite
datafile.write(' '); // (or .print(' ') Overwrite a blank at 58 position
datafile.flush(' '); // (or .close(' ') secure the write to file

The result is that the blank space is always written to the end of the file.

Can't you write to a selected byte in an SD file? I have seen this technique recommended on the Forum.

What is the trick? I don't get it.

Have you tried using search on this site for an answer? A good starting place might be to click on this link.

The trick should be to READ the byte first and then rewrite it.
Paul

Yes, I've looked all over the web as well. I looked over the article at the link you provided as well.

I've finally figured it out. There are undocumented flags you have to use during file.open() - O_RDWR is the one that allows you to seek() / write() after the open. FILE_WRITE flag opens disable the seek() function, only allow appends. There are other flags as well.

I don't know where a rerference document is for any of this. I think it may be in a library source file somewhere. Tribal knowledge.

	dataFile = SD.open(logfile, O_RDWR);
1 Like

Here is another forum article with relevant discussion.