CLOSE

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: CLOSE
Abbreviation: C, L, Shift+O
Type: Command
Token code: 160/$A0
Handling routine
in BASIC ROM:
57799–57811
$E1C7–E1D3
List of all BASIC keywords


Remark: This article describes the BASIC command CLOSE in BASIC V2 of the Commodore 64.

Type: Command
General Programming-Syntax: CLOSE <logical file number>

The BASIC command CLOSE is used for closing currently opened files or drive numbers, so that the data can be written from a write buffer to the actual device like tape or disk. If CLOSE is omitted after opening a file, the data of the file can be incomplete or even unreadable. As a rule, CLOSE should be used whenever an OPEN occurred and any of the BASIC commands CMD, GET#, INPUT# or PRINT# has been used, but no further file access is needed anymore.

The numerical values from 0 to 255 are valid logical file numbers. Any number out of this range leads to the BASIC error "?ILLEGAL QUANTITY ERROR. A close of an already closed file is silently ignored.

If a BASIC program stops all files are left open without closing. A restart (RUN) of the program could lead to some unexpected behavior. A re-open of an already open file might fail, at least partially. Neither OPEN itself nor a separate CLOSE right before the OPEN is able to prevent this situation under program control. A clean situation can only be achieved by an explicitly called CLOSE for the appropriate file number in direct mode.

Trivia[edit | edit source]

CLOSE follows the same parameter syntax as OPEN, but only the first parameter (the file number) is actually used. The following parameters are ignored - contrary to what some believe the comma-separated parameters aren't any further file numbers but according to the OPEN syntax the device number, secondary address.

Examples[edit | edit source]

Close logical file 1:

CLOSE 1

Close a file with logical file number in variable Z:

10 CLOSE Z

Z's value may be between 0 and 255.

20 CLOSE 1,15

Closes file number 1, the additional parameter is parsed according to the common OPEN syntax and would determine the device number, but none of the following parameters will be used.

Opens the keyboard device as a logical file for input:

200 OPEN 5,0: INPUT#5, A$: CLOSE5

This lets the user type something with a blinking cursor but without the question mark printed, which is the case for an INPUT from the default input file)

100 OPEN 4,4
110 CMD 4
120 LIST
130 PRINT#4,;
140 CLOSE 4

This program prints itself. The CMD output redirection has to be revoked with a PRINT# to ensure CLOSE will actually function as expected.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT