OPENIN

From BeebWiki
Jump to: navigation, search

OPENIN is a BASIC function to open a file for input.

OPENIN
Availability Present in all original versions of BBC BASIC.
Syntax BASIC I-V <num-var> = OPENIN(<string>)
Token (hex) BASIC I AD (function)
BASIC II-V E8 (function)
Description BASIC I Opens the named file for update (with OSFIND &C0), returning the

file handle or zero if the file was not found.

BASIC II-V Opens the named file for input (with OSFIND &40), returning the file

handle or zero if the file was not found.

Associated keywords OPENOUT, OPENUP,

EXT#, PTR#, INPUT#, PRINT#, BGET#, BPUT#, EOF#, CLOSE#

Description

OPENIN sets up the computer to work on an existing file. The file's name is given as the argument; BASIC passes the name to OSFIND, which tries to find the file on the current media, and in the current directory where appropriate.

If OSFIND finds the file it opens it, assigns it a file handle and returns the handle to BASIC, which returns it as the value of OPENIN. A file handle is an integer. 0 means the file was not found; otherwise the file handle is opaque, meaningful only to the MOS.

The file handle identifies the file while it is open, and the return value of OPENIN must be saved and passed to all file handling statements (those whose keywords end in #.) This is because more than one file may be open at a time. Unlike other BASICs the MOS assigns file handles, not the programmer.

On opening, the pointer (PTR#) associated with the file handle is initialised to zero. A file can be opened multiple times for input (while the filing system has enough remaining file handles) as long as it has not already been opened for output. Each file handle has its own independent pointer.

OPENIN in BASIC I vs BASIC II

In BASIC I, OPENIN opens files with OSFIND &C0, for reading and writing.

BASIC II was changed so that OPENIN opens files with OSFIND &40, for input only. The keyword OPENIN was assigned a new token, &8E, which would not work in BASIC I.

The keyword for the token &DA, formerly OPENIN was changed to OPENUP, relecting it's actual operation in using OSFIND &C0.

Therefore, BASIC I program files could be LOADed into BASIC II, and would contain OPENUP statements when LISTed.

Listings typed into BASIC II would use the new OPENIN token, and the program files would be incompatible with BASIC I.

See also OSFIND on this subject.

-- beardo 15:13, 21 March 2007 (UTC)