Hi,
I played around with those functions a bit and came up with this example, it assumes that you have a form with a File control on it called File1:
Private Sub Form_Load()
'Open the file '\temp\test.bin' for writing in 'random' (record) mode with a record length of 32 bytes.
File1.Open "\temp\test.bin", fsModeRandom, fsAccessWrite, fsLockReadWrite, 32
Dim i As Long
'Write 3 values to the file.
For i = 1 To 3 'Records are 1-based, writing a record with an index of 0 will work but you will get an error when reading it.
File1.Put Array(i, "Test " & i)
Next
File1.Close 'Close the file handle.
MsgBox "Test file written!", vbInformation
'Open the file '\temp\test.bin' for reading.
File1.Open "\temp\test.bin", fsModeRandom, fsAccessRead, fsLockWrite, 32
For i = 1 To 3
Dim varData As Variant
File1.Get varData, i 'Read the value of record 'i' into 'varData'.
MsgBox varData(0) & ": " & varData(1) 'Show the value of the record to the user.
Next
File1.Close
End Sub
You'll notice that the reading mode is set to
fsModeRandom, this lets us read records from the file given an index number.
It can be treated as a very simple type of database.
The last argument to Open is the length of our record, in bytes. You should pick a value that can comfortably accommodate the data you want to store, especially if you intend to store string values.
Normally with Visual Basic on the desktop, the common practice is to define the layout of a record in an UDT
(struct
) type. Unfortunately, eVB does not have support for UDTs, and the help file does not tell us what to use instead.
I assume that we are supposed to create a Variant array to store multiple values, as the help file does say this:
- You can use the Get method to read a Variant array from a disk, but you cannot use Get to read a scalar Variant containing an array. You also cannot use Get to read objects from a disk.
Also worthy of note is this remark:
- If the length of the data being read is less than the length specified in the Len clause of the Open method, Get reads subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data cannot be determined with certainty, it is advisable to match the record length with the length of the data being read.
I'm not entirely sure what they mean by this, but it may be worth padding your strings so the data is always the same length as the record to avoid any issues.
Also, this article for desktop VB may help if you want to learn more about 'random'
(record
) files:
Random Files.
Hope this helps!