Get Statement
Description
Reads data from an open disk file into a variable.
Syntax
Get [#]filenumber, [recnumber], varname
The Get statement syntax has these parts
Part | Description |
|
filenumber | Required. Any valid file number. |
recnumber | Optional. Variant (Long). Record number (Random mode files) or byte number (Binary mode files) at which reading begins. |
varname | Required. Valid variable name into which data is read. |
Remarks
Data read with Get is usually written to a file with Put.
The first record or byte in a file is at position 1, the second record or byte is at position 2, and so on. If you omit recnumber, 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, 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 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 reads 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.
For files opened in Binary mode, all of the Random rules apply, except:
See Also
Open statement, Put statement, Seek function, Type statement, VarType function.
Example
This example uses the Get statement to read data from a file into a variable. This example assumes 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, Position ' Declare variables.
' 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.