Overview (BinaryFile)
BytesToString (BinaryFile)
EmbedFile (BinaryFile)
Length (BinaryFile)
Offset (BinaryFile)
Position (BinaryFile)
ReadBool (BinaryFile)
ReadByte (BinaryFile)
ReadBytes (BinaryFile)
ReadDouble (BinaryFile)
ReadInt16 (BinaryFile)
ReadInt32 (BinaryFile)
ReadInt64 (BinaryFile)
ReadSingle (BinaryFile)
ReadString (BinaryFile)
RetrieveFile (BinaryFile)
RetrieveImage (BinaryFile)
StringToBytes (BinaryFile)
New1 (BinaryFile)
WriteBool (BinaryFile
WriteByte (BinaryFile)
WriteBytes (BinaryFile)
WriteBytes2 (BinaryFile)
WriteDouble (BinaryFile)
WriteInt16 (BinaryFile)
WriteInt32 (BinaryFile)
WriteInt64 (BinaryFile)
WriteSingle (BinaryFile)
WriteString (BinaryFile)
Overview (BinaryFile) Top
The BinaryFile library includes many methods for working with binary files.
Before using these methods you need to first add a reference to BinaryFile.dll and then create an object of this type.
Afterwards, initialize the object using New1.
The BinaryFile object receives a file connection (Stream) of a randomly opened file.
The methods include writing and reading different types of data from a file.
After reading or writing a value from the file, the file pointer moves forward to the next value.
Methods for setting the absolute or relative position of the file pointer (next byte to read).
And method for embedding and retrieving files to a file.
Example:
'First add an object named bin (of BinaryFile type).
Sub Globals
End Sub
Sub App_Start
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
bin.WriteString ("Product1")
bin.WriteDouble(123.23)
bin.WriteInt16(5)
bin.Position = 0 'Return the pointer to the start
productName = bin.ReadString
price = bin.ReadDouble 'Types must match
count = bin.ReadInt16
msgbox("Product: " & productName & crlf & "Price: " & price & crlf & "Amount: " & count)
FileClose(c1)
End Sub
BytesToString (BinaryFile) Top
Returns a string from an array of bytes using the BinaryFile object encoding.
Syntax: BytesToString (Buffer As Byte(), Index As Int32, Count As Int32) As String
Index - The start index.
Count - Number of bytes.
EmbedFile (BinaryFile) Top
Embeds a file in another file.
Used in conjunction with RetrieveFile or RetrieveImage.
Syntax: EmbedFile (FileName As String)
Example:
Sub App_Start
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
bin.EmbedFile (AppPath & "\pic.jpg")
bin.EmbedFile (AppPath & "\smiley.gif")
FileClose(c1)
End Sub
Length (BinaryFile) Top
Returns the size of the opened file.
Syntax: Length
Example:
Do While bin.Position < bin.Length
...
Loop
Offset (BinaryFile) Top
Moves the file pointer forward or backwards.
Syntax: Offset (i As Int64)
Example:
bin.Offset (4 * 10) 'Skips 10 Int32 numbers. (32 bit = 4 byte)
Position (BinaryFile) Top
Gets or sets the file pointer position.
Syntax: Position
Example:
Do While bin.Position < bin.Length
...
Loop
ReadBool (BinaryFile) Top
Reads a Boolean (True or False) value from a file.
Syntax: ReadBool
Example:
If bin.ReadBool = true then ...
ReadByte (BinaryFile) Top
Reads a Byte (0 - 255) value from a file.
Syntax: ReadBool
ReadBytes (BinaryFile) Top
Reads an array of Bytes from a file.
Syntax: ReadBytes (buffer As Byte(), count As Int32) As Int32
This method fills the array with the maximum of count bytes.
It returns the actual number of bytes read.
Example:
Sub Globals
Dim buffer(4096) As Byte
End Sub
Sub App_Start
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
length = bin.ReadBytes(buffer(), 4096)
FileClose(c1)
End Sub
ReadDouble (BinaryFile) Top
Reads a Double value (64 bit floating-point) from a file.
Syntax: ReadDouble
ReadInt16 (BinaryFile) Top
Reads a 16 bit integer from the file.
Syntax: ReadInt16
ReadInt32 (BinaryFile) Top
Reads a 32 bit integer from the file.
Syntax: ReadInt32
ReadInt64 (BinaryFile) Top
Reads a 64 bit integer from the file.
Syntax: ReadInt64
ReadSingle (BinaryFile) Top
Reads a Single value (32 bit floating-point) from a file.
Syntax: ReadSingle
ReadString (BinaryFile) Top
Reads a string from the file. The string is prefixed with the length.
Syntax: ReadString
RetrieveFile (BinaryFile) Top
Retrieves an embedded file from another file. Used in conjunction with EmbedFile.
Syntax: RetrieveFile (TargetFile As String)
This method creates a new file from the embedded file.
Example:
Sub App_Start
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
bin.RetrieveFile("1.jpg")
bin.RetrieveFile("2.gif")
FileClose(c1)
End Sub
RetrieveImage (BinaryFile) Top
Retrieves an image from an embedded file. Used in conjunction with EmbedFile.
Syntax: RetrieveImage
Returns a bitmap object.
Example:
Sub App_Start
form1.Show
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
Form1.Image = bin.RetrieveImage
Form1.DrawImage(bin.RetrieveImage,10,10)
FileClose(c1)
End Sub
StringToBytes (BinaryFile) Top
Returns an array of bytes from a string, using the Binary File object encoding.
Syntax: StringToBytes (String As String) As Byte()
Example:
Dim buffer(0) As Byte
buffer() = bin.StringToBytes("SomeString")
New1 (BinaryFile) Top
Initializes the object.
Syntax: New1 (ConnectionName As Stream, ASCII As Boolean)
ConnectionName - The name of a file connection opened using FileOpen (must be of random type).
ASCII - If false, strings will be encoded using UTF-8 (Unicode) format, otherwise strings will be encoded using ASCII format.
WriteBool (BinaryFile Top)
Writes a Boolean value to the file.
Syntax: WriteBool (b As Boolean)
Example:
bin.WriteBool (True)
WriteByte (BinaryFile) Top
Writes one Byte to the file.
Syntax: WriteByte (b As Byte)
WriteBytes (BinaryFile) Top
Writes an array of Bytes to the file.
Syntax: WriteBytes (buffer As Byte())
Example:
Sub Globals
Dim buffer(4096) As Byte
End Sub
Sub App_Start
FileOpen(c1,"data.dat",cRandom)
bin.New1(c1,true)
bin.WriteBytes(buffer())
FileClose(c1)
End Sub
WriteBytes2 (BinaryFile) Top
Writes part of an array of Bytes to the file.
Syntax: WriteBytes2 (buffer As Byte(), index As Int32, count As Int32)
index - The starting position (in the array).
count - Number of bytes to write.
WriteDouble (BinaryFile) Top
Writes a Double value (64 bit floating-point) to the file.
Syntax: WriteDouble (d As Double)
WriteInt16 (BinaryFile) Top
Writes a 16 bit integer to the file.
Syntax: WriteInt16 (i As Int16)
WriteInt32 (BinaryFile) Top
Writes a 32 bit integer to the file.
Syntax: WriteInt32 (i As Int32)
WriteInt64 (BinaryFile) Top
Writes a 64 bit integer to the file.
Syntax: WriteInt64 (i As Int64)
WriteSingle (BinaryFile) Top
Writes a Single value (32 bit floating-point) to the file.
Syntax: WriteSingle (i As Single)
WriteString (BinaryFile) Top
Writes a string prefixed with its length to the file.
Syntax: WriteString (s As String)
Example:
bin.WriteString("This is a string.")