Difference between revisions of "GET"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 40: Line 40:
  
  
''Example 3:'' Restoring [[Arrays|array]] data from a [[BINARY]] file using GET # in '''QB64''' only.
+
''Example 3:'' Placing the contents of a numerical array into a [[BINARY]] file. You may want to put the array size at the beginning too.
 +
{{CodeStart}} '' ''
 +
{{Cl|DIM}} {{Cl|SHARED}} array(100) {{Cl|AS}} {{Cl|INTEGER}}
  
{{CodeStart}}
+
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 100
DIM myarray(80000) AS {{Cl|INTEGER}}
+
  array(i) = i
  {{Cl|OPEN}} "myfile.ext" FOR {{Cl|BINARY}} AS #1  
+
{{Cl|NEXT}}
GET #1, , myarray()  
+
showme 'display array contents
CLOSE #1
+
 
 +
{{Cl|OPEN}} "BINFILE.BIN" {{Cl|FOR...NEXT|FOR}} {{Cl|BINARY}} {{Cl|AS}} #1
 +
 
 +
{{Cl|PUT}} #1, , array()
 +
 
 +
{{Cl|ERASE}} array 'clear element values from array and display empty
 +
showme
 +
{{Cl|CLOSE}} #1
 +
 
 +
{{Cl|OPEN}} "BINFILE.BIN" {{Cl|FOR...NEXT|FOR}} {{Cl|BINARY}} {{Cl|AS}} #2
 +
{{Cl|GET}} #2, , array()
 +
{{Cl|CLOSE}} #2
 +
showme  'display array after transfer from file
 +
 
 +
{{Cl|END}}
 +
 
 +
{{Cl|SUB}} showme
 +
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} 100
 +
  {{Cl|PRINT}} array(i);
 +
{{Cl|NEXT}}
 +
{{Cl|PRINT}} "done"
 +
{{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:'' The file data must have been previously stored in the file using PUT # the same way!
 
  
  

Revision as of 17:09, 26 August 2012

The GET # file or port device statement reads data by bytes or record positions.


Syntax

GET #filenumber%, [position][, {holdingvariable|holdingarray()}]


  • File/port number is the file 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 GETs are consecutive based on the holding variable TYPE byte size.
  • The holding variable TYPE or FIELD variable size determines the byte size and the next position in the file.
  • The first file position is 1. This may require adding one to an offset value when documentation uses that position as 0.
  • GET does NOT require a byte or record position or holding variable(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 holding array(include brackets).
  • GET can ignore an EOF statement and return bad data! EOF after a GET denotes that the data is bad!


Example 1: Checking for End of File before transferring individual GET data.

DO UNTIL EOF(1) GET #1, , value% IF NOT(EOF(1)) THEN PUT #2, , value% LOOP


Example 2: Using a TYPE record variable to read a TYPE record(Contact).

OPEN file$ FOR RANDOM as #1 LEN = LEN(Contact) IF record% <= LOF(1) \ LEN(Contact) THEN GET #1, record%, Contact CLOSE #1

Note: The Contact record contains all of the DOT variable types listed in the TYPE.


Example 3: 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:



Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page