Get (statement)

Syntax

Get [#] filenumber, [recordnumber], variable

Description

Retrieves data from a random or binary file and stores that data into the specified variable.

Comments

The Get statement accepts the following parameters:

 

Parameter

Description

 

filenumber

Integer used by the Basic Control Engine to identify the file. This is the same number passed to the Open statement.

 

recordnumber

Long specifying which record is to be read from the file.

For binary files, this number represents the first byte to be read starting with the beginning of the file (the first byte is 1). For random files, this number represents the record number starting with the beginning of the file (the first record is 1). This value ranges from 1 to 2147483647.

If the recordnumber parameter is omitted, the next record is read from the file (if no records have been read yet, then the first record in the file is read). When this parameter is omitted, the commas must still appear, as in the following example:

 

 

  Get #1,,recvar

 

 

If recordnumber is specified, it overrides any previous change in file position specified with the Seek statement.

 

variable

Variable into which data will be read. The type of the variable determines how the data is read from the file, as described below.

 

With random files, a runtime error will occur if the length of the data being read exceeds the reclen parameter specified with the Open statement. If the length of the data being read is less than the record length, the file pointer is advanced to the start of the next record. With binary files, the data elements being read are contiguous¾ the file pointer is never advanced.

 

Variable Types

The type of the variableparameter determines how data will be read from the file. It can be any of the following types:

 

Variable Type

File Storage Description

 

Integer

2 bytes are read from the file.

 

Long

4 bytes are read from the file.

 

String
(variable-length)

In binary files, variable-length strings are read by first determining the specified string variable's length and then reading that many bytes from the file. For example, to read a string of eight characters:

 

 

 s$ = String(8," ")
 Get #1,,s$

 

 

In random files, variable-length strings are read by first reading a 2-byte length and then reading that many characters from the file.

 

String
(fixed-length)

Fixed-length strings are read by reading a fixed number of characters from the file equal to the string's declared length.

 

Double

8 bytes are read from the file (IEEE format).

 

Single

4 bytes are read from the file (IEEE format).

 

Date

8 bytes are read from the file (IEEE double format).

 

Boolean

2 bytes are read from the file. Nonzero values are True, and zero values are False.

 

Variant

A 2-byte VarType is read from the file, which determines the format of the data that follows. Once the VarType is known, the data is read individually, as described above. With user-defined errors, after the 2-byte VarType, a 2-byte unsigned integer is read and assigned as the value of the user-defined error, followed by 2 additional bytes of information about the error.

The exception is with strings, which are always preceded by a 2-byte string length.

 

User-defined types

Each member of a user-defined data type is read individually

In binary files, variable-length strings within user-defined types are read by first reading a 2-byte length followed by the string's content. This storage is different from variable-length strings outside of user-defined types.

When reading user-defined types, the record length must be greater than or equal to the combined size of each element within the data type.

 

Arrays

Arrays cannot be read from a file using the Get statement.

 

Objects

Object variables cannot be read from a file using the Get statement.

 

 

 

Example

This example opens a file for random write, then writes ten records into the file with the values 10...50. Then the file is closed and reopened in random mode for read, and the records are read with the Get statement. The result is displayed in a message box.

Sub Main()
  Open "test.dat" For Random Access Write As #1
  For x = 1 to 10
    y = x * 10
    Put #1,x,y
  Next x
  Close

  Open "test.dat" For Random Access Read As #1
  msg1 = ""

  For y = 1 to 5
    Get #1,y,x
    msg1 = msg1 & "Record " & y & ": " & x & Basic.Eoln$
  Next y
  Close

  MsgBox msg1
End Sub

See Also

Open (statement); Put (statement); Input# (statement); Line Input# (statement); Input, Input$ (functions)

More information

G