Reading binary files

jgm

Member
Licensed User
Hi,
I need to read a dbase III file (*.dbf), change a few records and write it back.
As I do not know a better way to do it, I'm trying to open it as a binary file,
I tried reading help on binary files but couldn't get what I need to know.
Question1:
I managed to read one file, in binary mode, using ReadBytes, but got an error message when trying to assign what I read to a string using BytesToString (help file says BytesToString returns a string):
length = bin.ReadBytes(buffer(),5)
Version = bin.BytesToString(buffer(),1,3)
What am I missing?

Question2:
I have a text file that has just two chars "AB".
A is ASCII 65 (41HEX) and B is ASCII 66 (42HEX).
I read the file in binary mode using
in_data = bin.ReadInt16
and printed in_data in a MsgBox; got the decimal value 16961, which means (I think) that those bytes are dealt in reverse order "BA", otherwise I would get for "AB" = 41HEX 42HEX = 16706 decimal.
Is that so, I mean, are bytes in binary numbers written in reverse order?
 

agraham

Expert
Licensed User
What am I missing?
Might be useful to know what the error message and byte data was?
Is that so, I mean, are bytes in binary numbers written in reverse order?
Yes, numerics are usually written least significant byte first (on Wintel systems at least).
EDIT : That is true of binary numerics. I seem to remember (dredges deeply!) that in dBase files numerics are actually stored as strings.
 
Last edited:

jgm

Member
Licensed User
The file I'm reading is a text file with five chars "ABCDEF".

I believe I found what was happening.
I assigned what I read to a variable named Version and I got the message:
"Error compiling program.
Error description: Illegal first word in line.
Occured on line: 37
Version= bin.BytesToString(buffer(),1,3)"

Version must be a reserved word because after changing variable's name to VVersion I got no more error messages.

Thank you for your help.
 

jgm

Member
Licensed User
More than one Binary File open at the same time?

Is it possible to have more than one Binary File open at the same time, for example, one for reading and the other for writing?

If so, how can you tell which file you are referring to when using instructions like:
bin.Position
bin.ReadBytes
...
:sign0104:
 

klaus

Expert
Licensed User
You must define 2 binary file objects like and open the 2 files.

B4X:
  FileOpen(c1,file,cRandom)
  bin1.New1(c1,false)

  FileOpen(c2,file,cRandom)
  bin2.New1(c2,false)
This should work.

Best regards.
 
Top