Difference between revisions of "GET"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Clippy
m
Line 1: Line 1:
The '''GET #''' file or port statement reads data by bytes or record positions.
+
The '''GET #''' file or port device statement reads data by bytes or record positions.
  
  
Line 12: Line 12:
 
* The variable value is any string or number type or a [[TYPE]] or [[FIELD]] variable.
 
* The variable value is any string or number type or a [[TYPE]] or [[FIELD]] variable.
 
* '''QB64''' can GET the entire data contents of an array from a [[PUT]] array file.
 
* '''QB64''' can GET the entire data contents of an array from a [[PUT]] array file.
 +
* GET can ignore the EOF statement and return bad data when transferring data to another file! Always check for EOF '''after''' a GET to see if it should be [[PUT]].
  
  
 +
''Example 1: Checking for End of File before transferring individual GET data.
  
''Example 1:'' Using a TYPE record variable to read a TYPE record(Contact).
+
{{CodeStart}}
 +
DO UNTIL EOF(1)
 +
  GET #1, , value%
 +
  IF NOT(EOF(1)) THEN PUT #2, , value%
 +
LOOP
 +
{{CodeEnd}}
  
: OPEN file$ FOR RANDOM as #1 LEN = LEN(Contact)
+
''Example 2:'' Using a [[TYPE]] record variable to read a TYPE record(Contact).
: IF record% <= [[LOF]](1) \ LEN(Contact) THEN GET #1, record%, Contact
+
 
: CLOSE #1
+
{{CodeStart}}
 +
OPEN file$ FOR RANDOM as #1 LEN = LEN(Contact)
 +
IF record% <= [[LOF]](1) \ LEN(Contact) THEN GET #1, record%, Contact
 +
CLOSE #1
 +
{{CodeEnd}}
  
 
''Note:'' The Contact record contains all of the DOT variable types listed in the TYPE.
 
''Note:'' The Contact record contains all of the DOT variable types listed in the TYPE.
  
  
''Example 2:'' Restoring array data from a [[BINARY]] file using GET # in '''QB64''' only.
+
''Example 3:'' Restoring [[Arrays|array]] data from a [[BINARY]] file using GET # in '''QB64''' only.
  
:DIM myarray(80000) AS INTEGER  
+
{{CodeStart}}
:OPEN "myfile.ext" FOR BINARY AS #1  
+
DIM myarray(80000) AS INTEGER  
:GET #1, , myarray()  
+
OPEN "myfile.ext" FOR BINARY AS #1  
:CLOSE #1  
+
GET #1, , myarray()  
 +
CLOSE #1
 +
{{CodeEnd}}
  
 
''Note:'' The file data must have been previously stored using PUT the same way!
 
''Note:'' The file data must have been previously stored using PUT the same way!
Line 35: Line 48:
  
  
''See also:'' [[PUT|PUT #]], [[SEEK]], [[SEEK (statement)]], [[INPUT (file statement)|INPUT #]], [[GET (TCP/IP statement)]]
+
''See also:''  
  
 +
[[PUT|PUT #]], [[SEEK]], [[SEEK (statement)]]
  
 +
[[INPUT (file statement)|INPUT #]], [[GET (TCP/IP statement)]]
  
==Navigation:==
 
  
[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
 
  
[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
+
{{PageNavigation}}

Revision as of 17:59, 19 June 2010

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


Syntax: GET #filenumber%, [location], variable_value


  • File/port number is the number used in the OPEN AS statement.
  • The byte or record location is the byte in a BINARY file or the record in a RANDOM file.
  • Location can be omitted if the GETs are consecutive or when creating a new file.
  • The variable value is any string or number type or a TYPE or FIELD variable.
  • QB64 can GET the entire data contents of an array from a PUT array file.
  • GET can ignore the EOF statement and return bad data when transferring data to another file! Always check for EOF after a GET to see if it should be PUT.


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: Restoring array data from a BINARY file using GET # in QB64 only.

DIM myarray(80000) AS INTEGER OPEN "myfile.ext" FOR BINARY AS #1 GET #1, , myarray() CLOSE #1

Note: The file data must have been previously stored using PUT the same way!


See also:

PUT #, SEEK, SEEK (statement)

INPUT #, GET (TCP/IP statement)




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