Get #
Get statement reads data from a disk file into a variable.
The required FileNum argument is a system reference number associated with an open file. The optional RecNum argument is the byte position where the read starts for files opened in Binary mode. If you omit RecNum, the next record or byte following the last Get or Put statement (or pointed to by the last Seek function) is read. You must include delimiting commas.
The required VarName is the name of the variable where the file data is read (copied) to.
Random mode
For files opened in Random mode, the following rules apply:
Note: You can use the Get statement to read a Variant array from disk, but you can't use Get to read a scalar Variant containing an array. You also can't use Get to read objects from disk.
Get reads elements of user-defined types as if each were being read individually, except that there is no padding between elements. On disk, a dynamic array in a user-defined type (written with Put) is prefixed by a descriptor whose length equals 2 plus 8 times the number of dimensions, that is, 2 + 8 * NumberOfDimensions. The record length specified by the Lenclause in the Open statement must be greater than or equal to the sum of all the bytes required to read the individual elements, including any arrays and their descriptors.
Binary mode
For files opened in Binary mode, all of the Random rules apply, except:
Get reads variable-length strings that aren't elements of user-defined types without expecting the 2-byte length descriptor. The number of bytes read equals the number of characters already in the string.
Syntax
Get #(FileNum, RecNum, VarName)
FileNum:
An Integer or numeric expression representing any valid number in the range 1 to 511 inclusive, which is referenced by the file system to be associated with an open file.
RecNum:
The byte position where the read starts for files opened in Binary mode. If you omit RecNum, the next record or byte following the last Get or Put statement (or pointed to by the last Seek function) is read.
VarName:
A string representing a valid variable name.
Related Functions
GetAttr | Input | Line Input # | Print # | Put # | Write #
Example
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
Dim recRecord As Record
Dim intPosition As Integer
Dim intFileNum as Integer
intFileNum = FreeFile 'retrieve next free file number
' Open sample file for random access.
Open "TESTFILE.txt" For Random As #intFileNum
' Read the sample file using the Get statement.
intPosition = 3 ' Define third record number.
Get #intFileNum, intPosition, recRecord ' Read third record.
Close #intFileNum ' Close file.