Setting EOF on a SD file

I have a file that gets updated from time to time and some of the data inside the file changes length.

This requires rippling the data after the insertion up or down depending on the length of the new data.

[---data---|old value|---data---]EOF - original file (with old value in the middle)

  • new value arrives...
    [updated value] - to be inserted into the original file

[---data---|updated value|---data---]EOF - new file (with the longer updated value)

So all that is working OK, all ways; shorter to longer, longer to shorter and same length to same length.

The problem is that when it goes longer to shorter the file size shrinks but it still keeps the original length.
It will recognise the extra data when lengthened but not when shortened.
Subsequently the file size blows out in length over time. I need to be able to place a NEW end of file marker at the end, or rewrite the file size parameter. I have tried inserting EOF (-1 of 0xff) but this goes unnoticed.

The only way I can get it to change size to match the data content is to:

  1. Read the original file into a new empty file (copy)
  2. Delete the original file
  3. Make all my changes to the file copy
  4. Save this new file as the original file name

This could be abbreviated if there was a 'rename' function but alas, no.

So question time.... HOW do I set an end of file marker (EOF) into the file data???

You can write EOF (character 26) into the file and anything that treats the file as text will see that as the end of file. That won't help you though, because the blocks on the disk are still located to the file and the size will be unchanged.

Can you figure out what the largest set of data you need will be and keep the file at that size, accepting that shorter data will be wasting some space?

wildbill:
That won't help you though, because the blocks on the disk are still located to the file and the size will be unchanged.

Unless he uses an undocumented (by Arduino) truncate function. It's not available in the File class though, but on the more "obscured" SdFile class.
I don't remember the exact name, but I do know that you can actually shrink (truncate) a file at will.

Keep in mind if the file isn't shrunken enough, you won't gain any disk space if a "cluster" isn't freed up completely.