B4R Question SOLVED One issue in sd file update.

Discussion in 'B4R Questions' started by tigrot, May 14, 2018.

  1. tigrot

    tigrot Well-Known Member Licensed User

    Hi everybody,
    I'm writing a sub to update a record in a 51 records file. They are 18 bytes each.
    The sub is:

    Code:
    Sub writeprod(pezziin As ULong, allarmiin As ULong,prodottox As Int )
        
    Dim raf As RandomAccessFile
        
    Dim buffer(18As Byte
        raf.Initialize(buffer,
    True)
        
    Dim offset As Int = prodottox * 10 + 14
        
    Dim nomeprod() As Byte=leggiparam("macch",bc.StringToBytes(JoinStrings(Array As String"P"NumberFormat( offset + 10,1,0)))))
        Main.sd.Position = prodottox * buffer.Length
        
    Log("Scrive prodotto:",prodottox, " pos: ",prodottox*buffer.Length)
        
    'fill the buffer from the file
        Main.sd.Stream.ReadBytes(buffer, 0, buffer.Length)
        
    Dim r As Record
        raf.CurrentPosition = 
    0
        
    'read the data from the buffer
        r.pezzi = raf.ReadULong32( raf.CurrentPosition)
        r.allarmi = raf.ReadDouble32(raf.CurrentPosition)
        raf.CurrentPosition=
    0
        Main.sd.Position = prodottox * buffer.Length
        
    Log("sd.position:",Main.sd.Position)
        raf.WriteULong32(r.pezzi+pezziin,raf.CurrentPosition)
        raf.WriteULong32(r.allarmi+allarmiin,raf.CurrentPosition)
        raf.WriteBytes(nomeprod,
    0,nomeprod.Length,raf.CurrentPosition)
        Main.sd.Stream.WriteBytes(buffer,
    0,buffer.Length)
    End Sub
    The relevant log:
    Code:
    Scrive prodotto:1 pos: 18
    sd.position:
    18
    The strange is that record instead of being updated is appended to the file. I have tried with fresh created files, but I always get the same result. The record is written to the 52th record instead of the 2nd record (offset 18). If I rerun the sub the record is written at record 53!

    Best regards
    Mauro Zanin
     
  2. tigrot

    tigrot Well-Known Member Licensed User

    The new libraries in Arduino distribution seem to have a bug updating records in SD file. They always append the updated records.
    I had the most recent one 1.2.2. Just to give a try, I dowloaded the 1.1.1 and it works. I have found anything about this subject on Google.
    Hope that 1.1.1 is not buggy too somewhere!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice