B4R Question SOLVED One issue in sd file update.

tigrot

Well-Known Member
Licensed User
Longtime 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:

B4X:
Sub writeprod(pezziin As ULong, allarmiin As ULong,prodottox As Int )
    Dim raf As RandomAccessFile
    Dim buffer(18) As 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:
B4X:
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
 

tigrot

Well-Known Member
Licensed User
Longtime 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!
 
Upvote 0
Top