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.
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.