Shop OBEX P1 Docs P2 Docs Learn Events
How do I change the EOF using SD2.0_FATEngine.spin — Parallax Forums

How do I change the EOF using SD2.0_FATEngine.spin

Mike GMike G Posts: 2,702
edited 2010-12-26 12:17 in Propeller 1
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:
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

  • KyeKye Posts: 2,200
    edited 2010-12-21 11:17
    I don't understand what the problem is. You are describing how the system should operate.

    The SD2.0 Fatengine does not use EOF characters. if you want to know how long a file is use the "listSize" method.
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-12-21 11:37
    Guess he want's the file to be truncated when opening it.

    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.
  • Mike GMike G Posts: 2,702
    edited 2010-12-21 12:22
    Kye, first off, I'm NOT stating that there is a problem with SD2.0_FATEngine.spin. I'm simply trying to come up with a solution to my particular problem. My logic was, when I put put 100 bytes in a 50 byte file, the file grows to 100 bytes after it's closed. I assumed wrongly that the opposite must also be possible. I'll look for another solution as suggested by MagI02.
  • rokickirokicki Posts: 1,000
    edited 2010-12-23 17:01
    Kye wrote: »
    I don't understand what the problem is. You are describing how the system should operate.

    The SD2.0 Fatengine does not use EOF characters. if you want to know how long a file is use the "listSize" method.

    Well, it appears that opening with mode "w" is not truncating the file, as it should according to every
    other file system out there.
  • KyeKye Posts: 2,200
    edited 2010-12-24 09:27
    Mmm, I guess you found a bug!

    I'll fix that in the next release - Which will be very soon!
  • KyeKye Posts: 2,200
    edited 2010-12-26 12:14
    I just looked at my notes on my driver about this and its not actually a bug.

    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.
  • Mike GMike G Posts: 2,702
    edited 2010-12-26 12:17
    Thanks Kye, That's what I ended up doing, delete and recreate.
Sign In or Register to comment.