• Keine Ergebnisse gefunden

file handling

Im Dokument PICK BASIC reference manual (Seite 97-102)

The OPEN statement is used to select a PICK file for subsequent input. output or update.

Before a PICK file can be accessed by a READ. WRITE. DELETE. MATREAD, MATWlllTE. READV, or WITlV. etc •• statement. it must be opened via an OPEN

statement. The general form of the OPEN statement is:

OPEN {iOICT".} "file-oame" {TO file-variable} THEN/ELSE statements

The second expression in the OPEN statement indicates the PICK file name. If the first expression is "DICT", then the dictionary section of the file is opened. (The word DICT must be explicitly supplied to open a dictionary level file.) Note that either single or double quotes may be used in the statement.

Consider the following statements:

OPEN .... file-oame .... THEN/ELSE statements OPEN ... file-o ... THEN/ELSE statements

They are equivalent since the leading null expression is optional. In both cases, the data section is opened. If the file is a multiple data file (that is, multiple data files associated with a single dictionary), to open one of the data sections. the format used is:

"dict-name.data-name" or " ","dict-name,data-name"

If the "TO file-variable" option is used. then the dictionary or data section of the file will be assigned to the specified variable for subsequent

reference. If the "TO file-variable" option is omitted. then an internal default file-variable is generated; subsequent I/O statements not specifying a file-variable will then automatically default to this file.

Depending o~ whether the PICK file indicated in the OPEN statement exists, the

7

There is no limit to the number of files that may be open at any given time.

Consider the following example:

OPEN "DICT","QA4" TO Fl ELSE PRINT "NO FILE"; STOP printed and the program terminates.

Opens data section X of file ABC and assigns

7.2 CLEARING A FILE

7.2.1 CLEARFILE STATEMENT

The CLEARFILE statement is used to clear out the data section of a specified file. The general form of the CLEARFILE statement is:

CLEARFILE {file-variable}

Upon execution of the CLEARFILE statement, the data section of the file which was previously assigned to the specified file variable via an OPEN statement will be emptied. The data in the file will be deleted, but the file itself will not be deleted. If the file variable is omitted from the CLEARFILE statement, then the internal default variable is used (thus specifying the file most recently opened without a file-variable).

Consider the following example:

OPEN 'AFILE' TO X ELSE PRINT "CANNOT OPEN"; STOP CLEARFILE X

These statements cause th~ data section of the file named AFILE to be cleared.

The dictionary section of file cannot be cleared via a CLEARFILE statement.

Note that the BASIC program will abort with an appropriate error message if the specified file has not been opened prior to the execution of the CLEARFILE statement (refer to Appendix C, RUN-TIME ERROR MESSAGES).

Examples of the use of CLEARFILE:

Correct Use

OPEN 'FN1' ELSE PRINT 'NO FN1';STOP READ I FROM '11'ELSE STOP

CLEARFILE

OPEN 'FlLEA' TO A ELSE STOP OPEN 'FlLEB' TO B ELSE STOP CLEARFILE A

CLEARFILE B

OPEN 'ABC' ELSE PRINT 'NO FILE';STOP READV Q FROM 'IB3', 5 ELSE STOP IF Q-'TEST' THEN CLEARFILE

Incorrect Use CLEARFILE A+B CLEARFILE A,B

OPEN 'DICT','F5' TO C ELSE STOP CLEARFILE C

Explanation

Opens the data section of file FN1, reads item 11 and assigns value to variable I, and finally clears the data section of file FN1.

Clears the data sections of files FlLEA and FILEB.

Clears the data section of file ABC if the 5th attribute of the item with name IB3 has a string value of 'TEST'.

Explanation

A+B is not a legal variable.

Only one file can be cleared per CLEARFILE statement.

The dictionary section of a file cannot be cleared via a CLEARFILE statement.

7.3 ACCESSING FILE ITEMS 7 .3.1 READ STATEMENT

The READ statement reads a file item and assigns its value to a variable. The READ statement has the following general form:

READ variable FROM {file-variable,} item-name THEN/ELSE statements

The READ statement reads the file item specified by item-name and assigns its string value to the first variable. The file-variable is optional; if used, the item will be read from the file previously assigned to file-variable via an OPEN statement. If file-variable is omitted, then the internal default

variable is used (thus specifying the file most recently opened without a file- variable).

Depending on whether the item-name specifies the name of an item which exists, the statement or sequence of statements following the THEN/ELSE will be

executed. The statements in the THEN/ELSE clause may appear on one line separated by semicolons, or on multiple lines terminated by an END. The THEN/ELSE clause takes on the same format as the THEN/ELSE clause in the IF statement). Consider the example:

READ Xl FROM W,"TEMP" ELSE PRINT "NON-EXISTENT"; STOP

This statement will read the item named TEMP from the file opened and assigned to file-variable W, and will assign its string value to variable Xl; program control will then pass to the next sequential statement in the program. If the file item TEMP does not eXist, the message "NON-EXISTENT" will be printed and the program will terminate.

Note that the item-name should be surrounded by quotes if it directly

references an item, but that quotes are not needed if an indirect reference is used.

Note that the BASIC program will abort with an appropriate error message if the specified file has not been opened prior to the execution of the READ statement (refer· to Appendix C, RUN-TIME ERROR I{ESSAGES).

Examples of the use of READ:

Correct Use

READ Al FROM X," ABC" THEN PRINT "ABC"

GOTO 70 END A-"TEST"

B-"l"

Im Dokument PICK BASIC reference manual (Seite 97-102)