OPEN

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: OPEN
Abbreviation: O Shift+P
Type: Command
Token code: 159/9F
Handling routine
in BASIC ROM:
57790–57798
$E1BE–E1C6
List of all BASIC keywords


Remark: This article describes the BASIC command OPEN in BASIC V2 at the Commodore 64.

Type: Command
General programming syntax: 
 OPEN <logical filenumber> [,<device number> [,<secondary number> [,"<filename>[,<type>[,<mode>]]" ] ] ]

The BASIC command OPEN is used for opening a logical file, or a "channel" to a peripheral device, like a printer, datassette or disk drive, and for data input and output operations from external devices.

It is important to use OPEN before using any of the following BASIC commands: CLOSE, CMD, GET#, INPUT# and PRINT#. Furthermore, the OPEN command has to use the right file on the right device with the right logical filenumber with a value between 1 until 255 (reasonably below 128).

On the C64 each device has a separate device number, and in the OPEN command this number is used to identify the right peripheral device. If the device number is omitted, the first device — the datasette — will be used.

The use of the secondary number will vary from device to device and specifies what kind of device operation is to be executed, e.g. reading or writing data files or opening a special command channel. The secondary number is assumed to be 0 if omitted which causes a data file to be opened in read mode.

  • Device number 0 | keyboard (not counted as first device!)
  • Device number 1 | datasette | secondary number 0 = for loading; 1 = for writing; 2 = for writing a file followed by an EOT (End Of Tape) mark
  • Device number 2 | modem / RS-232 | secondary number 0
  • Device number 3 | screen | secondary number 0 or 1
  • Device number 4 or 5 | printer | secondary number 0 = capital letters/graphic symbols 7 = capital/small Letters
  • Device number 8 until 15 | disk drive | secondary number 2-14 = data channels[1]; 15 channel for commands

The following BASIC error messages can occur when using the device commands, whereas only the first 4 errors occur when using a datasette:

  • ?TOO MANY FILES: the limit of 10 concurrent opened files has been exceeded.
  • ?FILE NOT OPEN: one of the commands CLOSE, CMD, GET#, INPUT# or PRINT# tries to use a file that has not been open with the BASIC-command OPEN before.
  • ?FILE OPEN: the command OPEN tries to open an already open (and not closed) file again.
  • ?DEVICE NOT PRESENT: the selected device doesn't exist or is turned off; datasette: the file end (EOT) on the tape cannot be identified.
  • ?FILE NOT FOUND: no file of that name seems to exist (check spelling)
  • ?FILE EXISTS: occurs when giving a file a name already reserved for another file on that disk.

Examples[edit | edit source]

See also Drive command.

OPEN 50, 0 (another way for keyboard input)

OPEN 1, 1, 0, "FILENAME" 
(reading a file from datasette)

OPEN 1, 1, 1, "FILENAME"
(writing a file on datasette)

OPEN 1, 1, 2, "FILEEND"
(writing a file on datasette with file end (EOT))
OPEN 1, 2, 0, CHR$(10)
(open the RS-232 or modem)
OPEN 11, 3
(example output on screen)
OPEN 5, 4, "Control Character"
(use a control char at printer)

OPEN 4, 4, 0, "CAPITAL LETTERS"
(only capital letters on printer)

OPEN 3, 4, 7, "Capital-/Small Letters"
(capital-/small letters on printer)
OPEN 1, 8, 8, "FILENAME, SEQ, R"
(open a sequential file on the first disk drive for reading)

OPEN 2, 9, 4, "FILENAME, SEQ, W"
(open a sequential file on the second disk drive for writing)

OPEN 1, 8, 8, "FILENAME, REL, R"
(open a relative file on the first disk drive for reading)

OPEN 1, 8, 8, "FILENAME, USR, R"
(open a user file on the first disk drive for reading)


The file type specifications SEQ, REL, USR might be specified by their first letter only, i.e. S, R, U.
OPEN 1, 8, 15, "N:DISKNAME,ID"
(command for formatting a disk with name and ID.)

References[edit | edit source]

  1. Channel 0 and 1 are officially dedicated to LOAD and SAVE. They differ from normal channels in that the file type is always PRG and the mode is "read" for channel 0 and "write" for channel 1. With this convention the KERNAL does not need to add ",p,r" or ",p,w" to the filename. If this behavior suits your program, nothing stops you to use channel 0 and 1 in your own programs.


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