Siebel VB Language Reference > VB Language Reference >
This standard VB function reads data from a file opened in Random or Binary mode and puts it in a variable.
Get [#]filenumber, [recnumber], varName
The file number used in the Open statement to open the file
An expression of type long containing either the number of the record at which to start reading in Random mode, or the offset of the byte at which to start reading in Binary mode
The name of the variable into which Get reads file data; varName can be any variable except Object or Array variables, although single array elements can be used
For more information about how files are numbered when they are opened, read Open Statement.
The Recnumber argument is in the range 1 to 2,147,483,647. If this argument is omitted, the next record or byte is read.
NOTE: The commas before and after the recnumber are required, even if you do not supply a recnumber.
For Random mode, the following rules apply:
Blocks of data are read from the file in chunks whose size is equal to the size specified in the Len clause of the Open statement. If the size of varName is smaller than the record length, the additional data is discarded. If the size of varName is larger than the record length, an error occurs.
For variable length string variables, Get reads two bytes of data that indicate the length of the string, then reads the data into varName.
For variant variables, Get reads two bytes of data that indicate the type of the variant, then it reads the body of the variant into varName. Note that variants containing strings contain two bytes of data type information followed by two bytes of length followed by the body of the string.
User defined types are read as if each member were read separately, except no padding occurs between elements.
Files opened in Binary mode behave similarly to those opened in Random mode, except that:
- Get reads variables from the disk without record padding.
- Variable-length Strings that are not part of user-defined types are not preceded by the two-byte string length. Instead, the number of bytes read is equal to the length of varName.
This example opens a file for Random access, gets its contents, and closes the file again. The second subprogram,
createfile, creates the
c:\temp001 file used by the main subprogram.
Declare Sub CreateFile
' Put the numbers 1-10 into a file
Dim x as Integer
Open "c:\temp001" for Output as #1
For x = 1 to 10
Write #1, x
Dim acctno as String * 3
Dim recno as Long
Dim msgtext as String
recno = 1
newline = Chr(10)
Open "c:\temp001" For Random As #1 Len = 3
msgtext = "The account numbers are:" & newline
Do Until recno = 11
msgtext = msgtext & acctno
recno = recno + 1