OPENUP

From BeebWiki
Jump to: navigation, search

OPENUP is a BASIC function to open a file for update.

OPENUP
Availability Present in BASIC II onward, and available in BASIC I as

OPENIN.

Syntax BASIC II-V <num-var> = OPENUP(<string>)
Token (hex) BASIC II-V AD (function)
Description BASIC II-V Opens the named file for update (with OSFIND &C0), returning the

file handle or zero if the file was not found.

Associated keywords OPENIN, OPENOUT,

PTR#, EXT#, INPUT#</c ode>, <code>PRINT#, BGET#, BPUT#, EOF#, CLOSE#

Description

OPENUP 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 OPENUP. 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 OPENUP 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. All reading and writing is done at the pointer. If the programmer intends to append to the file, then the pointer must be set to the end of the file with PTR#handle=EXT#handle before any writing occurs.

'Opening for update' means opening for random access reading and writing. On most filing systems opening always succeeds as long as the file is present, regardless of the file's attributes. An error only occurs on writing to a read-only file when a write operation is attempted, and on reading from a write-only file when a read operation is attempted.

See also OSFIND on this subject.

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