How do I change the EOF using SD2.0_FATEngine.spin
Mike G
Posts: 2,702
I’m working out the details of creating a file system cache for my Spinneret project using SD2.0_FATEngine.spin.
I can read and write just fine. The problem comes in when the cache file is larger than the data I’m writing. For example, the file test.txt contains “This is a test”. I open the file and write “Hi”. Then I close the file. The file now contains “Hiis is a test”. The reverse works fine. If the file contains “Hi” and I write “This is a test” then close the file, the file now contains "This is a test" and the size is updated.
I goofed around for a while but I can’t figure out how to handle this situation. So, how do I redefine the end of a file using SD2.0_FATEngine.spin?
My test code:
I can read and write just fine. The problem comes in when the cache file is larger than the data I’m writing. For example, the file test.txt contains “This is a test”. I open the file and write “Hi”. Then I close the file. The file now contains “Hiis is a test”. The reverse works fine. If the file contains “Hi” and I write “This is a test” then close the file, the file now contains "This is a test" and the size is updated.
I goofed around for a while but I can’t figure out how to handle this situation. So, how do I redefine the end of a file using SD2.0_FATEngine.spin?
My test code:
PUB Main | i, temp, len, size, offset, s, flag pst.Start(115200) 'Initialize(eprom.start) SDCard.Start PauseMSec(2_000) 'pst.Str(header) flag := 1 pst.Char(CR) pst.Str(string("---------------------")) pst.Char(CR) { } pst.Str(string("Mount file system")) pst.Char(CR) SDCard.mount(fileErrorHandle) pst.Str(string("Open file")) pst.Char(CR) SDCard.openFile(string("test.txt"), "r") pst.Str(string("read file")) pst.Char(CR) size := SDCard.getFileSize SDCard.readFromFile(@fileBuffer, size) pst.Str(@fileBuffer) pst.Char(CR) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Close file")) pst.Char(CR) SDCard.closeFile pst.Str(string("File Size: ")) pst.Dec(size) pst.Char(CR) pst.Str(string("--------------")) pst.Char(CR) pst.Str(string("Open file test.txt")) pst.Char(CR) SDCard.openFile(string("test.txt"), "w") pst.Str(string("Fill file")) pst.Char(CR) if(flag == 0) SDCard.writeData(string("This is a test"), strsize(string("This is a test"))) else SDCard.writeData(string("Hi"), strsize(string("Hi"))) pst.Str(string("Close file")) pst.Char(CR) SDCard.closeFile pst.Str(string("Open the file again")) pst.Char(CR) SDCard.openFile(string("test.txt"), "r") size := SDCard.getFileSize SDCard.readFromFile(@fileBuffer, size) pst.Str(@fileBuffer) pst.Char(CR) SDCard.unmount(" ")
Comments
The SD2.0 Fatengine does not use EOF characters. if you want to know how long a file is use the "listSize" method.
I don't know the SD2.0 Fat engine, but I could imagine that it also can delete files? So, you simply have to delete the file first, then create it again. If I remember right that's what you have to do in DOS and other OS as well.
If that needs to much time you could use the first long of the file as actual size of valid data ignoring the file size.
Well, it appears that opening with mode "w" is not truncating the file, as it should according to every
other file system out there.
I'll fix that in the next release - Which will be very soon!
All files opened for writing or appending are opened in "RW" mode. All files opened for reading are opened in "R" mode.
So, you need to delete the file and make a new one if you want to shrink it.