Get Statement
Description
Reads from an open disk file into a variable.
Syntax
Get [#]filenumber,[recnumber],varname
The Get statement syntax has these parts:
Part |
Description |
|
filenumber |
Any valid file number. |
recnumber |
Record number (Random mode files) or byte number (Binary mode files) at which reading begins. |
varname |
Valid variable name into which data is read. |
Remarks
The first record/byte in a file is at position 1, the second record/byte is at position 2, and so on. If you omit recnumber, the next record or byte (the one after the last Get or Put statement or the one pointed to by the last Seek function) is read. Delimiting commas must be included, for example:
Get #4,,FileBuffer
For files opened in Random mode, the following rules apply:
- If the length of the data being read is less than the length specified in the Len clause of the Open statement, Get still 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 can't be determined with any certainty, it is generally a good idea to have the record length match the length of the data being read.
- If the variable being read into is a variable-length string, Get reads a 2-byte descriptor containing the string length and then the data that goes into the variable. Therefore, the record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual length of the string.
- If the variable being read into is a Variant of numeric type, Get reads 2 bytes identifying the VarType of the Variant and then the data that goes into the variable. For example, when reading a Variant of VarType 3, Get reads 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual number of bytes required to store the variable.
- If the variable being read into is a String Variant (VarType 8), Get reads 2 bytes identifying the VarType, 2 bytes indicating the length of the string, and then the string data. The record length specified by the Len clause in the Open statement must be at least 4 bytes greater than the actual length of the string.
- If the variable being read into is any other type of variable (not a variable-length string or a Variant), Get reads only the variable data. The record length specified by the Len clause in the Open statement must be greater than or equal to the length of the data being read.
- Get reads elements of user-defined types as if each were being read individually except that there is no padding between elements. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to read the individual elements.
For files opened in Binary mode, all of the Random rules apply except that:
See Also
Open Statement, Put Statement, Type Statement, VarType Function.
Example
This example uses the Get statement to read data from a disk file into a variable. For purposes of this example, assume that TESTFILE is a file containing five records of the user-defined type Record.
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type
Dim MyRecord As Record ' Declare variable.
' Open sample file for random access.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' Read the sample file using the Get statement.
Position = 3 ' Define record number.
Get #1, Position, MyRecord ' Read third record.
Close #1 ' Close file.