x
This website is using cookies. We use cookies to ensure that we give you the best experience on our website. More info. That's Fine
HPC:Factor Logo 
 
Latest Forum Activity

How do I use "put"? (eVB)

I dunk for bananas Page Icon Posted 2024-02-21 3:59 PM
#
Avatar image of I dunk for bananas
H/PC Elite

Posts:
707
Location:
Europe
Status:
My current method of utilizing small "database" files is quite hacking and essentially just delimiting records by newline when writing, and then to fetch data, split by newline again. I read about the "put" command in the documentation but I'm a bit confused as to how to actually use it. Does anyone have a very simple example of how I could for example, write the contents of the variable "test" to disk, and then read it again?
 Top of the page
WinCEDev Page Icon Posted 2024-02-22 12:28 AM
#
Avatar image of WinCEDev
Factorite (Senior)

Posts:
77
Location:
Europe
Status:
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!
 Top of the page
I dunk for bananas Page Icon Posted 2024-03-29 8:43 PM
#
Avatar image of I dunk for bananas
H/PC Elite

Posts:
707
Location:
Europe
Status:
Thank you so much! Sorry for the late reply I don't suppose there's a way to delete a record again?
 Top of the page
Jump to forum:
Seconds to generate: 0.156 - Cached queries : 61 - Executed queries : 8