FIELD: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
m (Protected "FIELD" ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite)))
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 7: Line 7:


{{PageDescription}}
{{PageDescription}}
* {{Parameter|fileNumber%}} is a file number used in the [[OPEN]] statement or a value from the [[FREEFILE]] function.  
* {{Parameter|fileNumber%}} is a file number used in the [[OPEN]] statement or a value from the [[FREEFILE]] function.
* Combined size of the {{Parameter|fieldWidth%}} parameters '''must not exceed the [[LEN]] = recordsize in the [[RANDOM]] [[OPEN]] statement''' or a [[ERROR Codes|"FIELD overflow" error]] will occur.
* Combined size of the {{Parameter|fieldWidth%}} parameters '''must not exceed the [[LEN]] = recordsize in the [[RANDOM]] [[OPEN]] statement''' or a [[ERROR Codes|"FIELD overflow" error]] will occur.
* Variables are limited to [[STRING]] types. Use [[TYPE]] instead of FIELD if you want to use numerical values.  
* Variables are limited to [[STRING]] types. Use [[TYPE]] instead of FIELD if you want to use numerical values.
* Once a [[FIELD]] is defined in a statement, [[GET]] can read and [[PUT]] can write data without placeholders or variables.
* Once a [[FIELD]] is defined in a statement, [[GET]] can read and [[PUT]] can write data without placeholders or variables.
* [[LSET]], [[RSET]], [[PRINT (file statement)|PRINT #]], [[PRINT USING (file statement)|PRINT # USING]], and [[WRITE (file statement)|WRITE #]] can be used to place characters in the file buffer before a [[PUT]].
* [[LSET]], [[RSET]], [[PRINT (file statement)|PRINT #]], [[PRINT USING (file statement)|PRINT # USING]], and [[WRITE (file statement)|WRITE #]] can be used to place characters in the file buffer before a [[PUT]].
Line 18: Line 18:
{{PageExamples}}
{{PageExamples}}
''Example:'' Comparing a [[TYPE]] definition with a FIELD [[STRING|string]] definition. Demo using a [[TYPE]] definition to create a file:
''Example:'' Comparing a [[TYPE]] definition with a FIELD [[STRING|string]] definition. Demo using a [[TYPE]] definition to create a file:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|TYPE}} ClientType
{{Cl|TYPE}} ClientType
   CName {{Cl|AS}} {{Cl|STRING}} * 30    '30 bytes
   CName {{Cl|AS}} {{Cl|STRING}} * 30    '30 bytes
   Address {{Cl|AS}} {{Cl|STRING}} * 30  '30 bytes  
   Address {{Cl|AS}} {{Cl|STRING}} * 30  '30 bytes
   City  {{Cl|AS}} {{Cl|STRING}} * 15    '15 bytes
   City  {{Cl|AS}} {{Cl|STRING}} * 15    '15 bytes
   State  {{Cl|AS}} {{Cl|STRING}} * 2    ' 2 bytes
   State  {{Cl|AS}} {{Cl|STRING}} * 2    ' 2 bytes
Line 36: Line 36:
   {{Cl|IF}} CName$ = "END" {{Cl|THEN}} {{Cl|EXIT DO}}
   {{Cl|IF}} CName$ = "END" {{Cl|THEN}} {{Cl|EXIT DO}}
   record = record + 1              'increment record number
   record = record + 1              'increment record number
   Client.CName = CName$        
   Client.CName = CName$
   Client.Address = Address$
   Client.Address = Address$
   Client.City = City$
   Client.City = City$
Line 43: Line 43:
   {{Cl|PUT}} #1, record, Client    'PUT by record number
   {{Cl|PUT}} #1, record, Client    'PUT by record number
{{Cl|LOOP}}
{{Cl|LOOP}}
{{Cl|CLOSE}} #1  
{{Cl|CLOSE}} #1
{{Cl|END}}
{{Cl|END}}


Line 52: Line 52:
   {{Cl|DATA}} "Wyley Coyote","33 Roadrunner Ave.","Clairton","PA","15122"
   {{Cl|DATA}} "Wyley Coyote","33 Roadrunner Ave.","Clairton","PA","15122"
   {{Cl|DATA}} "Jim Morrison","19 Doorway Dr.","Belleview","PA","15236"
   {{Cl|DATA}} "Jim Morrison","19 Doorway Dr.","Belleview","PA","15236"
   {{Cl|DATA}} "END",0,0,0,0 '' ''
   {{Cl|DATA}} "END",0,0,0,0
{{CodeEnd}}
{{CodeEnd}}


Demo using the FIELD statement to read the file:
Demo using the FIELD statement to read the file:
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|CONST}} NM = 30, AD = 30, CT = 15, ST = 2, ZC = 5  ' Define field and record lengths with constants.
{{Cl|CONST}} NM = 30, AD = 30, CT = 15, ST = 2, ZC = 5  ' Define field and record lengths with constants.
{{Cl|CONST}} RLEN = NM + AD + CY + ST + ZC
{{Cl|CONST}} RLEN = NM + AD + CY + ST + ZC
Line 71: Line 71:
       Info$ = Clist$
       Info$ = Clist$
       {{Cl|PRINT}} {{Cl|LEFT$}}(Info$, 30)    'read name string
       {{Cl|PRINT}} {{Cl|LEFT$}}(Info$, 30)    'read name string
       {{Cl|PRINT}} {{Cl|MID$}}(Info$, 31, 30)  'read address string
       {{Cl|PRINT}} {{Cl|MID$ (function)|MID$}}(Info$, 31, 30)  'read address string
       {{Cl|PRINT}} {{Cl|RIGHT$}}(Info$, 17)    'read city, state and zip code
       {{Cl|PRINT}} {{Cl|RIGHT$}}(Info$, 17)    'read city, state and zip code
       {{Cl|PRINT}}
       {{Cl|PRINT}}
Line 78: Line 78:
{{Cl|LOOP}}
{{Cl|LOOP}}
{{Cl|CLOSE}} #1
{{Cl|CLOSE}} #1
{{Cl|END}} '' ''
{{Cl|END}}
{{CodeEnd}}
{{CodeEnd}}



Latest revision as of 00:34, 26 February 2023

The FIELD statement creates a STRING type definition for a random-access file buffer.


Syntax

FIELD [#]fileNumber&, fieldWidth1% AS variable1$[, fieldWidthN% AS variableN$]


Description

  • fileNumber% is a file number used in the OPEN statement or a value from the FREEFILE function.
  • Combined size of the fieldWidth% parameters must not exceed the LEN = recordsize in the RANDOM OPEN statement or a "FIELD overflow" error will occur.
  • Variables are limited to STRING types. Use TYPE instead of FIELD if you want to use numerical values.
  • Once a FIELD is defined in a statement, GET can read and PUT can write data without placeholders or variables.
  • LSET, RSET, PRINT #, PRINT # USING, and WRITE # can be used to place characters in the file buffer before a PUT.
  • All field definitions for a file are removed when the file is closed or RESET and all strings are set to null ("").
  • Do not re-assign a field defined variable value or use it in an INPUT statement if you want the variable to remain a field.


Examples

Example: Comparing a TYPE definition with a FIELD string definition. Demo using a TYPE definition to create a file:

TYPE ClientType
   CName AS STRING * 30     '30 bytes
   Address AS STRING * 30   '30 bytes
   City   AS STRING * 15    '15 bytes
   State  AS STRING * 2     ' 2 bytes
   Zip    AS STRING * 5     ' 5 bytes
END TYPE      ' total size = 82 bytes
DIM Client AS ClientType
RecordLEN = LEN(Client)       'find the size of each TYPE record

OPEN "ADDRESS.DAT" FOR RANDOM AS #1 LEN = RecordLEN
RESTORE ClientData         'restore to start of DATA
record = 0
DO
   READ CName$, Address$, City$, State$, Zip$       'read DATA
   IF CName$ = "END" THEN EXIT DO
   record = record + 1               'increment record number
   Client.CName = CName$
   Client.Address = Address$
   Client.City = City$
   Client.State = State$
   Client.Zip = Zip$
   PUT #1, record, Client     'PUT by record number
LOOP
CLOSE #1
END

ClientData:
   DATA "Bob White","104 Birdland Rd.","Bellview","PA","15236"
   DATA "Ward Cleaver","123 W. Beaver St.","Beaver","PA","15255"
   DATA "Elmer Fudd","45 Wabbit St.","Bethel Park","PA","15022"
   DATA "Wyley Coyote","33 Roadrunner Ave.","Clairton","PA","15122"
   DATA "Jim Morrison","19 Doorway Dr.","Belleview","PA","15236"
   DATA "END",0,0,0,0

Demo using the FIELD statement to read the file:

CONST NM = 30, AD = 30, CT = 15, ST = 2, ZC = 5  ' Define field and record lengths with constants.
CONST RLEN = NM + AD + CY + ST + ZC
'
OPEN "ADDRESS.DAT" FOR RANDOM AS #1 LEN = RLEN
FIELD #1, NM AS CName$, AD AS Address$, CY AS City$, ST AS State$, ZC AS Zip$
FIELD #1, RLEN AS Clist$         'define entire record

GET #1, 1                  'GET does not need a variable to read FIELD records!
                                  'Read file for zip codes from 15230 to 15239 .
DO WHILE NOT EOF(1)
   ZipCheck$ = Zip$                            'read zip codes
   IF (ZipCheck$ >= "15230" AND ZipCheck$ <= "15239") THEN
      Info$ = Clist$
      PRINT LEFT$(Info$, 30)     'read name string
      PRINT MID$(Info$, 31, 30)  'read address string
      PRINT RIGHT$(Info$, 17)    'read city, state and zip code
      PRINT
   END IF
   GET #1                               'simply GET reads each FIELD record after first
LOOP
CLOSE #1
END


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link