GET: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
m (Removed protection from "GET") |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
{{PageDescription}} | {{PageDescription}} | ||
* {{Parameter|fileNumber&}} is the file or port number used in the [[OPEN]] AS [[BINARY]] or [[RANDOM]] statement. | * {{Parameter|fileNumber&}} is the file or port number used in the [[OPEN]] AS [[BINARY]] or [[RANDOM]] statement. | ||
* The [[INTEGER]] or [[LONG]] byte {{Parameter|position}} in a [[BINARY]] file or the record {{Parameter|position}} in a [[RANDOM]] file '''must be greater than zero'''. | * The [[INTEGER]] or [[LONG]] byte {{Parameter|position}} in a [[BINARY]] file or the record {{Parameter|position}} in a [[RANDOM]] file '''must be greater than zero'''. | ||
* The {{Parameter|position}} can be omitted if the GET operations are consecutive based on the {{Parameter|targetVariable}} [[TYPE]] byte size. | * The {{Parameter|position}} can be omitted if the GET operations are consecutive based on the {{Parameter|targetVariable}} [[TYPE]] byte size. | ||
* The {{Parameter|targetVariable}} [[Data types|type]] or [[FIELD]] ''variable'' size determines the byte size and the next {{Parameter|position}} in the file. | * The {{Parameter|targetVariable}} [[Data types|type]] or [[FIELD]] ''variable'' size determines the byte size and the next {{Parameter|position}} in the file. | ||
* The first byte position in a file is 1. | * The first byte position in a file is 1. | ||
* GET does not require a byte or record {{Parameter|position}} or {{Parameter|targetVariable}} (or comma) when using a [[FIELD]] statement. | * GET does not require a byte or record {{Parameter|position}} or {{Parameter|targetVariable}} (or comma) when using a [[FIELD]] statement. | ||
* '''QB64''' can [[PUT]] the entire contents of an array to a file and later GET those contents to a {{Parameter|targetArray()}} (include brackets). | * '''QB64''' can [[PUT]] the entire contents of an array to a file and later GET those contents to a {{Parameter|targetArray()}} (include brackets). | ||
Line 24: | Line 24: | ||
{{PageExamples}} | {{PageExamples}} | ||
''Example 1:'' Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size. | ''Example 1:'' Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|TYPE}} variabletype | {{Cl|TYPE}} variabletype | ||
x {{Cl|AS}} {{Cl|INTEGER}}' '2 bytes | x {{Cl|AS}} {{Cl|INTEGER}}' '2 bytes | ||
Line 54: | Line 54: | ||
{{Cl|PRINT}} newrec.x, newrec.y, newrec.z | {{Cl|PRINT}} newrec.x, newrec.y, newrec.z | ||
{{Cl|END}} | {{Cl|END}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} 16 bytes | {{OutputStart}} 16 bytes | ||
Line 66: | Line 66: | ||
''Example 2:'' Placing the contents of a numerical array into a [[BINARY]] file. You may want to put the array size at the beginning too. | ''Example 2:'' Placing the contents of a numerical array into a [[BINARY]] file. You may want to put the array size at the beginning too. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DIM}} {{Cl|SHARED}} array(100) {{Cl|AS}} {{Cl|INTEGER}} | {{Cl|DIM}} {{Cl|SHARED}} array(100) {{Cl|AS}} {{Cl|INTEGER}} | ||
Line 94: | Line 94: | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|PRINT}} "done" | {{Cl|PRINT}} "done" | ||
{{Cl|END SUB}} | {{Cl|END SUB}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
: ''Note:'' Use empty brackets in QB64 when using [[GET]] to create an array or [[PUT]] to create a [[BINARY]] data file. | : ''Note:'' Use empty brackets in QB64 when using [[GET]] to create an array or [[PUT]] to create a [[BINARY]] data file. | ||
Line 100: | Line 100: | ||
{{PageSeeAlso}} | {{PageSeeAlso}} | ||
* [[PUT|PUT #]], [[SEEK]], [[SEEK ( | * [[PUT|PUT #]], [[SEEK]], [[SEEK (function)]] | ||
* [[INPUT (file statement)|INPUT #]], [[GET (TCP/IP statement)]] | * [[INPUT (file statement)|INPUT #]], [[GET (TCP/IP statement)]] | ||
* [[FIELD]], [[RANDOM]], [[BINARY]] | * [[FIELD]], [[RANDOM]], [[BINARY]] |
Latest revision as of 23:51, 24 February 2023
The GET # statement reads data from a file or port device by bytes or record positions.
Syntax
- GET #fileNumber&, [position][, {targetVariable|targetArray()}]
Description
- fileNumber& is the file or port number used in the OPEN AS BINARY or RANDOM statement.
- The INTEGER or LONG byte position in a BINARY file or the record position in a RANDOM file must be greater than zero.
- The position can be omitted if the GET operations are consecutive based on the targetVariable TYPE byte size.
- The targetVariable type or FIELD variable size determines the byte size and the next position in the file.
- The first byte position in a file is 1.
- GET does not require a byte or record position or targetVariable (or comma) when using a FIELD statement.
- QB64 can PUT the entire contents of an array to a file and later GET those contents to a targetArray() (include brackets).
- GET may ignore the end of a file and return bad data. If the EOF function returns -1 after a GET operation, it indicates that the data has ended.
DO UNTIL EOF(1) GET #1, , value% IF NOT(EOF(1)) THEN PUT #2, , value% LOOP |
Examples
Example 1: Opening a RANDOM file using LEN to calculate and LEN = to designate the file record size.
TYPE variabletype x AS INTEGER' '2 bytes y AS STRING * 10' '10 bytes z AS LONG' '4 bytes END TYPE' '16 bytes total DIM record AS variabletype DIM newrec AS variabletype file$ = "testrand.inf" '<<<< filename may overwrite existing file number% = 1 '<<<<<<<<<< record number to write cannot be zero RecordLEN% = LEN(record) PRINT RecordLEN%; "bytes" record.x = 255 record.y = "Hello world!" record.z = 65535 PRINT record.x, record.y, record.z OPEN file$ FOR RANDOM AS #1 LEN = RecordLEN% PUT #1, number% , record 'change record position number to add records CLOSE #1 OPEN file$ FOR RANDOM AS #2 LEN = RecordLEN% NumRecords% = LOF(2) \ RecordLEN% PRINT NumRecords%; "records" GET #2, NumRecords% , newrec 'GET last record available CLOSE #2 PRINT newrec.x, newrec.y, newrec.z END |
16 bytes 255 Hello worl 65535 1 records 255 Hello worl 65535 |
- Explanation: The byte size of the record TYPE determines the LOF byte size of the file and can determine the number of records.
- To read the last record GET the number of records. To add a record, use the number of records + 1 to PUT new record data.
Example 2: Placing the contents of a numerical array into a BINARY file. You may want to put the array size at the beginning too.
DIM SHARED array(100) AS INTEGER FOR i = 1 TO 100 array(i) = i NEXT showme 'display array contents OPEN "BINFILE.BIN" FOR BINARY AS #1 PUT #1, , array() ERASE array 'clear element values from array and display empty showme CLOSE #1 OPEN "BINFILE.BIN" FOR BINARY AS #2 GET #2, , array() CLOSE #2 showme 'display array after transfer from file END SUB showme FOR i = 1 TO 100 PRINT array(i); NEXT PRINT "done" END SUB |
- Note: Use empty brackets in QB64 when using GET to create an array or PUT to create a BINARY data file.
See also