Hi everybody,
I'm writing a sub to update a record in a 51 records file. They are 18 bytes each.
The sub is:
The relevant log:
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
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
B4X:
Scrive prodotto:1 pos: 18
sd.position:18
Best regards
Mauro Zanin