Writing and deleting text on SD card with sdfat lib


I am working on a project which involves arduino mega 2560, esp8266 and SD card module.

The idea is, a user can send some text commands to the device through the esp8266 module. The esp8266 is interfaced through serial with Arduino.
Some of these text commands will be some sort of schedule or anything else. On the SD card they are being stored as distinct lines with newlines at the end. So after each '\n' the there might be another newline or "End of File".

I have been able to successfully append the texts on the card and read them char by char.

But now I need to find a text line in the file and need to replace it with another string or simply delete it (which can be also another replace operation with just a newline character).

So far I could come up with is the SdFile::curPosition(), seekset() and similar methods.

I have been also able to see which position in the file i m reading though I m not sure how to manipulate these methods to replace or delete chars from the text file.

I am using the sdfat lib because I also have some methods that allows me to wipe or format the SDcard from the micro controller which is a needed feature for my project.

Have anyone done it? Any code example or advice is appreciated.

The way to delete text from a file is to read the part of the text you want to keep, and write it to another file. Read and discard the part you don't want. Read and write the rest of the file to the other file. Delete the original file, and rename the copy.

Unless you can determine a maximum length for a record, and make all records the same length, and don't mind a marker that says "this record was deleted" as a place holder, there are no other choices.