Get Statement
Reads data into a variable from an open file on the disk.
Syntax
Get [#]filenumber, [recnumber], varname |
The Get statement syntax contains the following parts:
Элемент |
Описание |
filenumber |
Requied, any valid file number. |
recnumber |
Optional, of Variant (Long) type.
Sets the record number (for files in the Random mode) or byte number
(for files in the Binary mode) from which to start reading. |
varname |
Required, a valid name of the variable in which
the read data will be stored. |
Remarks
Data, read using the Get statement are normally written to a file with
the Put statement.
Number 1 corresponds to the first record (or byte) of the file, number 2 - to
the second one, and so on. If the recnumber argument is omitted, reading
starts from the record (byte) to which the pointer has been moved after the
most recent Get or Put operation (or
where it has been moved after the last Seek
function call). The comma separators are required, for instance:
Get #4,,FileBuffer
The following rules apply to the files, opened in the Random mode:
- Even if the lenght of data to be read is less than the lenght of the record,
specified in the Len parameter of the Open
statement, the Get statement starts reading each subsequent record
from the beginning of this record. The space between the end of one record
and the start of the following one gets filled with the contents of the file
buffer. As it's hard to calculate exactly the amount of data, used for filling,
it's recommended that the record lenght be the same as the lenght of data
being read.
- If data is read to a string of variable lenght, the Get statement
first reads the 2-byte descriptor indicating the string lenght, and then the
data to be put into the variable. So the record lenght specified in the Len
parameter of the Open statement must be at least 2 byte greater than
the actual string lenght.
- If data is read into a Variant variable of numeric type, the Get
statement first reads the 2 bytes indicating the subtype (VarType)
of this variable, and then the data to be put into this variable. For instance,
when reading the Variant variable of VarType 3 subtype the Get
statement reads 6 bytes: 2 байта indicating the subtype of the Variant
variable as VarType 3 (Long), and 4 bytes containing the value
of the Long type. The record lenght specified in the Len parameter
of the Open statement must be at least
2 byte greater than the actual size needed to store this variable.
The above rules apply to files opened in the Binary mode, except of
the following:
- The Len parameter of the Open
statement is ignored. Get reads all variables from the disk continuously
- i.e. without filling the space between records with file buffer contents.
- When reading any arrays, except for those which are elements of user-defined
types, the Get statement reads only data. The descriptor is not read.
- When reading strings of variable lenght which are not elements of user-defined
types the 2-byte descriptor is not read. The number of bytes being read is
equal to the number of symbols contained in the string. The statements below
read 10 bytes from the file with number 1:
VarString = String(10," ")
Get #1,,VarString
Example
In this example the Get statement is used for reading data from a file
into a variable. It's assumed that the TESTFILE file contains 5 records (see
the example of using Put).
Dim sName As String * 20, nPosition ' Declares variable.
' Opens file for random access. Open "TESTFILE" For Random As #1 Len = 21
' Reads from the file using the Get statement. nPosition = 3 ' Determines record number. Get #1, nPosition, sName ' Reads the third record.
MsgBox(sName) Close #1 ' Closes file.
|