Q(uick)BASIC Statement: FIELD

Quick View

FIELD

A file I/O statement that allocates space for variables in a random-access file buffer

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • FIELD [#]filenumber%, fieldwidth% AS stringvariable$ [,fieldwidth% AS stringvariable$] …
Description/Parameter(s)
filenumber% The number of an open file.
fieldwidth% The number of characters in the field.
stringvariable$ A variable that identifies the field and contains field data.
  • Record variables usually provide a better way to handle record data.

Differences from BASICA

  • When a random-access file is closed in BASICA with a CLOSE or RESET statement, field variables retain the last value assigned to them by a GET statement. In QBasic, all field variables are reset to null strings.
Example
OPEN "FILEDAT.DAT" FOR RANDOM AS #1 LEN = 80 FIELD #1, 30 AS name$, 50 AS address$
Syntax
  • FIELD [#]filenumber, fieldwidth AS stringvariable [,fieldwidth AS stringvariable] …
Description/Parameter(s)
Argument Description
filenumber The number used in the file's OPEN statement
fieldwidth The width of the field in the record
stringvariable The string variable that contains the date read from a record or data that is used in an assignment when information is written to a record
  • The total number of bytes that you allocate in a FIELD statement must not exceed the record length that you had specified when opening the file. Otherwise, an error message is generated that reads "FIELD overflow." (The default record length is 128 bytes.)
  • Any number of FIELD statements may be executed for the same file. All FIELD statements that have been executed remain in effect at the same time.
  • All field definitions for a file are removed when the file is closed; that is, all strings defined as fields associated with the file are set to null.
  • Do not use a variable name defined as a field in an INPUT or assignment statement if you wish the variable to remain a field. Once a variable name is a field, it points to the correct place in the random-access file buffer. If a subsequent INPUT or assignment statement with that variable name is executed, the variable's pointer no longer refers to the random-access record buffer, but to string space.
Note: BASIC's record variables and extended OPEN statement syntax provide a more convenient way to use random-access files.

Differences from BASICA

  • When a random-access file is closed with a CLOSE or RESET statement in a compiled program, all variables that are fields associated with that file are reset to null strings. When a random-access file is closed in a BASICA program, variables that are fields retain the last value assigned to them by a GET statement.
Example

This example illustrates a random-access file buffer with multiple definitions: In the first FIELD statement, the 67-byte buffer is broken up into five separate variables for name, address, city, state, and zip code.In the second FIELD statement, the same buffer is assigned entirely to one variable, Plist$. The remainder of this example checks to see if Zip$, which contains the zip code, falls within a certain range; if it does, the complete address string is printed.

' Example program for the FIELD statement TYPE Buffer FuName AS STRING * 25 Addr AS STRING * 25 City AS STRING * 10 State AS STRING * 2 Zip AS STRING * 5 END TYPE DIM RecBuffer AS Buffer '******************************************************************** ' NOTE: This part of the program creates a random-access ' file for use by the second part of the program, which ' demonstrates the FIELD statement '******************************************************************** OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = LEN(RecBuffer) CLS RESTORE READ FuName$, Addr$, City$, State$, Zip$ I = 0 DO WHILE UCASE$(FuName$) <> "END" I = I + 1 RecBuffer.FuName = FuName$ RecBuffer.Addr = Addr$ RecBuffer.City = City$ RecBuffer.State = State$ RecBuffer.Zip = Zip$ PUT #1, I, RecBuffer READ FuName$, Addr$, City$, State$, Zip$ IF FuName$ = "END" THEN EXIT DO LOOP CLOSE #1 ' DATA "Bob Hartzell","1200 Liberty St.","Bow","WA","98232" DATA "Alice Provan","123 B St.","Bellevue","WA","98005" DATA "Alex Landow","14900 123rd","Bothell","WA","98011" DATA "Walt Riley","33 Minnow Lake Road","Lyman","WA","98263" DATA "Georgette Gump","400 15th W.","Bellevue","WA","98007" DATA "END",0,0,0,0,0 '********************************************************************* 'This part of the program demonstrates the use of the FIELD statement '********************************************************************* ' Define field and record lengths with constants. CONST FU = 25, AD = 25, CT = 10, ST = 2, ZP = 5 CONST RECLEN = FU + AD + CT + ST + ZP ' OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = RECLEN FIELD #1, FU AS FuName$, AD AS Addr$, CT AS City$, ST AS State$, ZP AS Zip$ FIELD #1, RECLEN AS Plist$ GET #1, 1 ' Read the file, looking for zip codes in the range 98000 to 98015. DO WHILE NOT EOF(1) Zcheck$ = Zip$ IF (Zcheck$ >= "98000" AND Zcheck$ <= "98015") THEN Info$ = Plist$ PRINT LEFT$(Info$, 25) PRINT MID$(Info$, 26, 25) PRINT RIGHT$(Info$, 17) PRINT END IF GET #1 LOOP CLOSE #1
Syntax
  • FIELD [#]filenumber%, fieldwidth% AS stringvariable$ [,fieldwidth% AS stringvariable$] …
Description/Parameter(s)
  • The argument filenumber% is the number used in the OPEN statement to open the file.
  • The argument stringvariable$ is a string variable that contains the DATA read from a record, or data used in an assignment when information is written to a record.

Usage Notes

  • The total number of bytes that you allocate in a FIELD statement must not exceed the record length that you had specified when opening the file. Otherwise, BASIC generates the error message, "FIELD overflow." (The default record length is 128 bytes.)
  • Any number of FIELD statements can be executed for the same file. All FIELD statements that have been executed remain in effect at the same time.
  • All field definitions for a file are removed when the file is closed; that is, all strings defined as fields associated with the file are set to null.

Important

  • Do not use a variable name defined as a field in an INPUT or assignment statement if you wish the variable to remain a field. Once a variable name is a field, it points to the correct place in the random-access file buffer. If a subsequent INPUT or assignment statement with that variable name is executed, the variable's pointer no longer refers to the random-access record buffer, but to string space.

Programming Tips

  • BASIC's record variables and extended OPEN statement syntax provide a more convenient way to use random-access files.

Differences from BASICA

  • When a random-access file is closed with a CLOSE or RESET statement in a compiled program, all variables that are fields associated with that file are reset to null strings. When a random-access file is closed in a BASICA program, variables that are fields retain the last value assigned to them by a GET statement.
Examples

This example uses the FIELD statement to define a random-access file buffer. In the first FIELD statement, the 67-byte buffer is broken up into five separate variables for name, address, city, state, and zip code. In the second FIELD statement, the same buffer is assigned entirely to one variable, Plist$. The program then checks to see if Zip$, which contains the zip code, falls within a certain range; if it does, the complete address string is printed.

TYPE Buffer FuName AS STRING * 25 Addr AS STRING * 25 City AS STRING * 10 State AS STRING * 2 Zip AS STRING * 5 END TYPE DIM RecBuffer AS Buffer

Note: This part of the program creates a random-access file for use by the second part of the program, which demonstrates the FIELD statement.

OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = LEN(RecBuffer) CLS RESTORE READ FuName$, Addr$, City$, State$, Zip$ I = 0 DO WHILE UCASE$(FuName$) <> "END" I = I + 1 RecBuffer.FuName = FuName$ RecBuffer.Addr = Addr$ RecBuffer.City = City$ RecBuffer.State = State$ RecBuffer.Zip = Zip$ PUT #1, I, RecBuffer READ FuName$, Addr$, City$, State$, Zip$ IF FuName$ = "END" THEN EXIT DO LOOP CLOSE #1 DATA "Bob Hartzell","1200 Liberty St.","Bow","WA","98232" DATA "Alice Provan","123 B St.","Bellevue","WA","98005" DATA "Alex Landow","14900 123rd","Bothell","WA","98011" DATA "Walt Riley","33 Minnow Lake Road","Lyman","WA","98263" DATA "Georgette Gump","400 15th W.","Bellevue","WA","98007" DATA "END",0,0,0,0,0

This part of the program demonstrates use of the FIELD statement.

'Define field and record lengths with constants. CONST FU = 25, AD = 25, CT = 10, ST = 2, ZP = 5 CONST RECLEN = FU + AD + CT + ST + ZP OPEN "MAILLIST.DAT" FOR RANDOM AS #1 LEN = RECLEN FIELD #1, FU AS FuName$, AD AS Addr$, CT AS City$, ST AS State$, ZP AS Zip$ FIELD #1, RECLEN AS Plist$ GET #1, 1 'Read the file, looking for zip codes in the range 98000 to 98015. DO WHILE NOT EOF(1) Zcheck$ = Zip$ IF (Zcheck$ >= "98000" AND Zcheck$ <= "98015") THEN Info$ = Plist$ PRINT LEFT$(Info$, 25) PRINT MID$(Info$, 26, 25) PRINT RIGHT$(Info$, 17) PRINT END IF GET #1 LOOP CLOSE #1