Q(uick)BASIC Statement (File I/O): GET
Quick View
GET
A file I/O statement that reads from a disk file into a random-access buffer or variable
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- GET [#]filenumber%[,[recordnumber&][,variable]]
- PUT [#]filenumber%[,[recordnumber&][,variable]]
Description/Parameter(s)
filenumber% | The number of an open file. |
recordnumber& | For random-access files, the number of the record to read or write. For binary-mode files, the byte position where reading or writing starts. |
variable | For GET, a variable used to receive input from the file. For PUT, a variable that contains output to write to the file. The variable is usually a variable of a user-defined data type. |
Example
TYPE TestRecord
Student AS STRING * 20
Score AS SINGLE
END TYPE
DIM MyClass AS TestRecord
OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass)
MyClass.Student = "MarySa"
MyClass.Score = 99
PUT #1, 1, MyClass
CLOSE #1
OPEN "FINAL.DAT" FOR RANDOM AS #1 LEN = LEN(MyClass)
GET #1, 1, MyClass
PRINT "STUDENT:", MyClass.Student
PRINT "SCORE:", MyClass.Score
CLOSE #1
KILL "FINAL.DAT"
Syntax
- GET [#]filenumber[,[recordnumber][,variable]]
Description/Parameter(s)
Argument | Description |
filenumber | The number used in the OPEN statement to open the file. |
recordnumber | For random-access files, the number of the record to be read. For binary-mode files, the byte position in the file where reading starts. The first record or byte position in a file is 1. If you omit recordnumber, the next record or byte (the one after the last GET or PUT, or the one pointed to by the last SEEK) is read into the buffer. The largest possible record number is 2^31 -1, or 2,147,483,647. |
variable | The variable used to receive input from the file. If you use a variable, you do not need to use CVD, CVL, CVI, or CVS to convert record fields to numbers. You may not use a FIELD statement with the file if you use the variable argument. |
For random-access files, you can use any variable as long as the length of the variable is less than or equal to the length of the record. Usually, a record variable defined to match the fields in a data record is used. | |
For binary-mode files, you can use any variable. The GET statement reads as many bytes as there are in the variable. | |
When you use a variable-length string variable, the statement reads as many bytes as there are characters in the string's value. For example, the following two statements read 10 bytes from file number 1: | |
|
|
See the examples for more information about using variables rather than FIELD statements for random- access files. A record cannot be longer than 32,767 bytes. |
You may omit the recordnumber, the variable, or both. If you omit the recordnumber but include the variable, you must still include the commas:
- GET #4,,FileBuffer
If you omit both arguments, you do not include the commas:
- GET #4
The GET and PUT statements allow fixed-length input and output for BASIC communications files. Use GET carefully because if there is a communications failure, GET waits indefinitely for recordnumber characters.
Note: | When you use GET with the FIELD statement, you can use INPUT # or LINE INPUT # after a GET statement to read characters from the random-access file buffer. You may use the EOF function after a GET statement to see if the GET went beyond the end of the file. |
Other Uses of the GET Keyword
- ⮜ GET (Graphics) ⮞ - to copy a rectangular area of the screen by translating the image to numeric data and storing that data in an array
Example
The following program opens the file TESTDAT2.DAT for random access and displays the contents on the screen.
' Read and display the contents of a file containing a
' name of up to 20 characters and a test score.
'
' Define record fields.
TYPE TestRecord
NameField AS STRING * 20
ScoreField AS SINGLE
END TYPE
' Define a variable of the user type.
DIM Rec AS TestRecord
'********************************************************************
' This part of the program is an insert whose only function is to
' create a random-access file to be used by the second part of the
' program, which demonstrates the CVSMBF function
'********************************************************************
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(Rec)
CLS
RESTORE
READ NameField$, ScoreField
I = 0
DO WHILE UCASE$(NameField$) <> "END"
I = I + 1
Rec.NameField = NameField$
Rec.ScoreField = ScoreField
PUT #1, I, Rec
READ NameField$, ScoreField
IF NameField$ = "END" THEN EXIT DO
LOOP
CLOSE #1
'
DATA "John Simmons", 100
DATA "Allie Simpson", 95
DATA "Tom Tucker", 72
DATA "Walt Wagner", 90
DATA "Mel Zucker", 92
DATA "END", 0
' Open the test data file.
'
DIM FileBuffer AS TestRecord
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN=LEN(FileBuffer)
' Calculate number of records in the file.
Max = LOF(1) / LEN(FileBuffer)
' Read and print contents of each record.
FOR I = 1 TO Max
GET #1, I, FileBuffer
PRINT FileBuffer.NameField, FileBuffer.ScoreField
NEXT I
CLOSE #1
END
See also:
Syntax
- GET [#]filenumber%[,[recordnumber&][,variable]]
Description/Parameter(s)
- The argument filenumber% is the number used in the OPEN statement to open the file.
- If you omit recordnumber&, the next record or byte (the one after the last GET or PUT, or the one pointed to by the last SEEK) is read into the buffer. The largest possible record number is 2^31 -1, or 2,147,483,647.
- The first record or byte position in a file is 1.
- A record cannot be longer than 32,767 bytes.
- If you specify a variable, you do not need to use CVI, CVL, CVS, CVD, or CVC to convert record fields to numbers. You can not use a FIELD statement with the file if you use the variable argument.
- For random-access files, you can use any variable as long as the length of the variable is less than or equal to the length of the record. Usually, a record variable defined to match the fields in a DATA record is used.
- For binary-mode files, you can use any variable. The GET statement reads as many bytes as there are in the variable.
- When you use a variable-length string variable, the statement reads as many bytes as there are characters in the string's value. For example, the following two statements read 10 bytes from file number 1:
- VarStrings$ = STRING$(10, " ")
- GET #1, , VarString$
- See the examples for more information about using variables rather than FIELD statements for random-access files.
Usage Notes
- Do not use GET on ISAM files.
- You can omit the arguments recordnumber&, variable, or both. If you omit the recordnumber& but include a variable, you must still include the commas:
- GET #4, , FileBuffer
- If you omit both arguments, do not include the commas:
- GET #4
Important
- GET and PUT statements allow fixed-length input and output for BASIC communications files. Use GET carefully because if there is a communications failure, GET waits indefinitely for recordnumber& characters.
Programming Tips
- When you use GET with the FIELD statement, you can use INPUT # or LINE INPUT # after a GET statement to read characters from the random-access file buffer. You may use the EOF function after a GET statement to see if the GET went beyond the end of the file.
Example
This example uses the GET statement to display the contents of a newly created random-access file that contains 5 names and corresponding test scores.
'Define record fields.
TYPE TestRecord
NameField AS STRING * 20
ScoreField AS SINGLE
END TYPE
'Define a variable of the user type.
DIM Rec AS TestRecord
DIM I AS LONG
'Note: This part of the program is an insert whose function is to
'create a random-access file to be used by the second part of the
'program, which demonstrates the GET statement.
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(Rec)
CLS
RESTORE
READ NameField$, ScoreField
I = 0
DO WHILE NameField$ <> "END"
I = I + 1
Rec.NameField = NameField$
Rec.ScoreField = ScoreField
PUT #1, I, Rec
READ NameField$, ScoreField
LOOP
CLOSE #1
DATA "John Simmons", 100
DATA "Allie Simpson", 95
DATA "Tom Tucker", 72
DATA "Walt Wagner", 90
DATA "Mel Zucker", 92
DATA "END", 0
'End of insert.
'Open the test data file.
DIM FileBuffer AS TestRecord
DIM Max AS LONG
OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer)
'Calculate number of records in the file.
Max = LOF(1) \ LEN(FileBuffer)
'Read and print contents of each record.
FOR I = 1 TO Max
GET #1, I, FileBuffer
PRINT FileBuffer.NameField, FileBuffer.ScoreField
NEXT I
CLOSE #1
KILL "TESTDAT2.DAT"
END