LIFO stack on SD card

Hi,

I'm trying to write a LIFO stack (i.e. Last In First Out) to a file on an SD card.

Getting the data in is easy, I just open the file in write mode and append a new record to the end. Reading the data out again is slightly harder, I search for the penultimate carriage return & line feed, and read the last line.

What I'm stuck on is how to actually remove (delete) the last line from the file? The file can have several hundred records in it, and I want to remove the last one. I know I could just copy the entire file except for the last record but this would be using a hammer to crack an egg.

The really annoying thing is that in the early days of SDFat there used to be a file.truncate() function which would allow a file to be truncated from a given position forward. How can I achieve the same using the current SD library? Is there an EOF (End-of-file) indicator that I can write in at the start of the last line to chop the end off?

Thanks

(this is related to a similar post here)

I've attempted something similar, and I don't think it's possible anymore to 'shrink' the size of a file. Two options....

  1. Don't attempt to remove the last line, just overwrite with blank spaces.
  2. Create your own end-of-file marker, edit this into the file, and move it each time you add/remove a line

I search for the penultimate carriage return & line feed, and read the last line.

I do believe that that is the first time I've seen penultimate used correctly.

Rather than try to truncate the file, why not just write an "this-is-the-end-of-my-data" marker to the file, where you want your code to stop reading?

  1. Create your own end-of-file marker, edit this into the file

why not just write an "this-is-the-end-of-my-data" marker to the file

These are both basically the same thing, this might be possible, the problem with this is that the data in the file is encoded data that uses all the available ascii characters, so there's the danger that whatever end-of-data marker I choose may occur naturally within the data.

It will be really annoying if I do have to use this work-around, because I know the file.truncate() function did and I expect it's still buried in there somewhere if I was clever enough to work out how to get to it.

It will be really annoying if I do have to use this work-around, because I know the file.truncate() function did and I expect it's still buried in there somewhere if I was clever enough to work out how to get to it.

Post in the Storage section (or ask a moderator to move this), where fat16lib hangs out.