Q(uick)BASIC Statement: RESTORE

Quick View

RESTORE

An I/O statement that allows DATA statements to be reread from a specified line

Worth knowing

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

Syntax
  • DATA constant[,constant]…
  • READ variablelist
  • RESTORE [line]
Description/Parameter(s)

DATA specifies values to be read by subsequent READ statements.
READ reads those values and assigns them to variables.
RESTORE allows READ to reread values in specified DATA statements.

constant One or more numeric or string constants specifying the data to be read. String constants containing commas, colons, or leading or trailing spaces are enclosed in quotation marks (" ").
variablelist One or more variables, separated by commas, that are assigned data values. Variable names can consist of up to 40 characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.).
line The label or line number of a DATA statement. If line is omitted, the next READ statement reads values in the first DATA statement in the program.
  • DATA statements can be entered only at the module level. They cannot be used in procedures.
Example
FOR i% = 1 TO 3 READ a%, b$ PRINT a%, b$ RESTORE NEXT i% DATA 1, "Repeat"
Syntax
  • RESTORE [{linelabel | linenumber}]
Description/Parameter(s)
  • If the argument is omitted, the next READ statement which executes will read the first item in the first DATA statement in the program
  • linelabel or linenumber identifies the DATA statement you want the next READ statement to use. The first item from that line will be read.

After executing a RESTORE statement without a specified linelabel or linenumber, the next READ statement gets the first item in the first DATA statement in the program.

If linenumber or linelabel is specified, the next READ statement gets the first item in the specified DATA statement. If a line is specified, the line label must be in the module-level code. (Note that in the QuickBASIC environment, DATA statements are automatically moved to the module-level code.)

Example

QCARDS.BAS, the program used in the QuickBASIC tutorial, uses the RESTORE statement extensively. It is included on the Setup distribution diskette. To look at the QCARDS.BAS program text and see how the RESTORE statement is used, load it using the File menu's Open Program command. To run QCARDS.BAS, you must follow the instructions in the tutorial.

Syntax
  • RESTORE [{linelabel | linenumber}]
Description/Parameter(s)
linelabel or linenumber Identifies the DATA statement you want the next READ statement to use. The first item from that line will be read.
If the argument is omitted, the next READ statement that executes will read the first item in the first DATA statement in the program.

Usage Notes

  • After executing a RESTORE statement without a linelabel or linenumber, the next READ statement gets the first item in the first DATA statement in the program.
  • If linenumber or linelabel is specified, the next READ statement gets the first item in the specified DATA statement. If a line is specified, the line label must be in the module-level code. (Note that in the QBX environment, DATA statements are automatically moved to the module-level code.)
Example

The following program uses the SEEK function and the SEEK statement to move the file position exactly one record back and rewrite the record if a variable is true (nonzero). The RESTORE statement is used to set the data pointer to the first DATA statement.

OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(RecordVar) CLS RESTORE READ NameField$, ScoreField I = 0 DO WHILE NameField$ <> "END" I = I + 1 RecordVar.NameField = NameField$ RecordVar.ScoreField = ScoreField PUT #1, I, RecordVar READ NameField$, ScoreField LOOP CLOSE #1 DATA "John Simmons", 100 DATA "Allie Simpson", 95 DATA "Tom Tucker", 72 DATA "Walt Wagner", 90 DATA "Mel Zucker", 92 DATA "END", 0 'Open the test data file. DIM FileBuffer AS TestRecord OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer) 'Calculate number of records in the file. Max = LOF(1) \ LEN(FileBuffer) 'Read contents of each record. FOR I = 1 TO Max GET #1, I, FileBuffer IF FileBuffer.NameField = "Tom Tucker" THEN ReWriteFlag = TRUE EXIT FOR END IF NEXT I IF ReWriteFlag = TRUE THEN 'Back up file by the length of the record variable that 'is used to write to the file. FileBuffer.ScoreField = 100 SEEK #1, SEEK(1) - LEN(RecordVar) PUT #1, , RecordVar END IF CLOSE #1 KILL "TESTDAT2.DAT" END