mirror of
https://github.com/QB64Official/qb64.git
synced 2024-08-31 12:20:16 +00:00
90 lines
No EOL
4.6 KiB
Text
90 lines
No EOL
4.6 KiB
Text
The [[FIELD]] statement creates a [[STRING]] type definition for a [[RANDOM|random]]-access file buffer.
|
|
|
|
|
|
{{PageSyntax}}
|
|
: [[FIELD]] [#]{{Parameter|fileNumber&}} {{Parameter|fieldWidth1%}} AS {{Parameter|variable1$}}[, {{Parameter|fieldWidthN%}} AS {{Parameter|variableN$}}]
|
|
|
|
|
|
{{PageDescription}}
|
|
* {{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.
|
|
* 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 (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]].
|
|
* All field definitions for a file are removed when the file is [[CLOSE|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'''.
|
|
|
|
|
|
{{PageExamples}}
|
|
''Example:'' Comparing a [[TYPE]] definition with a FIELD [[STRING|string]] definition. Demo using a [[TYPE]] definition to create a file:
|
|
{{CodeStart}} '' ''
|
|
{{Cl|TYPE}} ClientType
|
|
CName {{Cl|AS}} {{Cl|STRING}} * 30 '30 bytes
|
|
Address {{Cl|AS}} {{Cl|STRING}} * 30 '30 bytes
|
|
City {{Cl|AS}} {{Cl|STRING}} * 15 '15 bytes
|
|
State {{Cl|AS}} {{Cl|STRING}} * 2 ' 2 bytes
|
|
Zip {{Cl|AS}} {{Cl|STRING}} * 5 ' 5 bytes
|
|
{{Cl|END}} {{Cl|TYPE}} ' total size = 82 bytes
|
|
{{Cl|DIM}} Client {{Cl|AS}} ClientType
|
|
RecordLEN = {{Cl|LEN}}(Client) 'find the size of each TYPE record
|
|
|
|
{{Cl|OPEN}} "ADDRESS.DAT" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = RecordLEN
|
|
{{Cl|RESTORE}} ClientData 'restore to start of DATA
|
|
record = 0
|
|
{{Cl|DO}}
|
|
{{Cl|READ}} CName$, Address$, City$, State$, Zip$ 'read DATA
|
|
{{Cl|IF}} CName$ = "END" {{Cl|THEN}} {{Cl|EXIT DO}}
|
|
record = record + 1 'increment record number
|
|
Client.CName = CName$
|
|
Client.Address = Address$
|
|
Client.City = City$
|
|
Client.State = State$
|
|
Client.Zip = Zip$
|
|
{{Cl|PUT}} #1, record, Client 'PUT by record number
|
|
{{Cl|LOOP}}
|
|
{{Cl|CLOSE}} #1
|
|
{{Cl|END}}
|
|
|
|
ClientData:
|
|
{{Cl|DATA}} "Bob White","104 Birdland Rd.","Bellview","PA","15236"
|
|
{{Cl|DATA}} "Ward Cleaver","123 W. Beaver St.","Beaver","PA","15255"
|
|
{{Cl|DATA}} "Elmer Fudd","45 Wabbit St.","Bethel Park","PA","15022"
|
|
{{Cl|DATA}} "Wyley Coyote","33 Roadrunner Ave.","Clairton","PA","15122"
|
|
{{Cl|DATA}} "Jim Morrison","19 Doorway Dr.","Belleview","PA","15236"
|
|
{{Cl|DATA}} "END",0,0,0,0 '' ''
|
|
{{CodeEnd}} '' ''
|
|
|
|
Demo using the FIELD statement to read the file:
|
|
{{CodeStart}} '' ''
|
|
{{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|OPEN}} "ADDRESS.DAT" {{Cl|FOR (file statement)|FOR}} {{Cl|RANDOM}} {{Cl|AS}} #1 {{Cl|LEN}} = RLEN
|
|
{{Cl|FIELD}} #1, NM {{Cl|AS}} CName$, AD {{Cl|AS}} Address$, CY {{Cl|AS}} City$, ST {{Cl|AS}} State$, ZC {{Cl|AS}} Zip$
|
|
{{Cl|FIELD}} #1, RLEN {{Cl|AS}} Clist$ 'define entire record
|
|
|
|
{{Cl|GET}} #1, 1 'GET does not need a variable to read FIELD records!
|
|
'Read file for zip codes from 15230 to 15239 .
|
|
{{Cl|DO}} {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(1)
|
|
ZipCheck$ = Zip$ 'read zip codes
|
|
{{Cl|IF}} (ZipCheck$ >= "15230" {{Cl|AND (boolean)|AND}} ZipCheck$ <= "15239") {{Cl|THEN}}
|
|
Info$ = Clist$
|
|
{{Cl|PRINT}} {{Cl|LEFT$}}(Info$, 30) 'read name string
|
|
{{Cl|PRINT}} {{Cl|MID$}}(Info$, 31, 30) 'read address string
|
|
{{Cl|PRINT}} {{Cl|RIGHT$}}(Info$, 17) 'read city, state and zip code
|
|
{{Cl|PRINT}}
|
|
{{Cl|END IF}}
|
|
{{Cl|GET}} #1 'simply GET reads each FIELD record after first
|
|
{{Cl|LOOP}}
|
|
{{Cl|CLOSE}} #1
|
|
{{Cl|END}} '' ''
|
|
{{CodeEnd}}
|
|
|
|
|
|
{{PageSeeAlso}}
|
|
* [[OPEN]], [[TYPE]]
|
|
* [[GET]], [[PUT]]
|
|
* [[LSET]], [[RSET]]
|
|
|
|
|
|
{{PageNavigation}} |