qb45advr.hlp (Topic list)
OPEN (File I/O) Statement Details
  QuickSCREEN      Details      Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
OPEN (File I/O) Statement Details
 
Syntax
  OPEN file [FOR mode1] [ACCESS access] [lock] AS [#]filenum [LEN=reclen]
 
The file is a string expression that specifies an optional device, followed
by a file name or path name conforming to the DOS file-naming conventions.
 
You must open a file before any I/O operation can be performed on it. OPEN
allocates a buffer for I/O to the file or device and determines the mode
of access used with the buffer.
 
Syntax 1
 
In the first syntax, mode1 is one of the following:
 
  Mode     Description
  OUTPUT   Specifies sequential output mode.
 
  INPUT    Specifies sequential input mode.
 
  APPEND   Specifies sequential output mode and sets the file pointer
           to the end of file and the record number to the last record
           of the file. A PRINT # or WRITE # statement then extends
           (appends to) the file.
 
  RANDOM   Specifies random-access file mode, the default mode. In
           RANDOM mode, if no ACCESS clause is present, three attempts
           are made to open the file when the OPEN statement is
           executed. Access is attempted in the following order:
 
           1. Read/write
           2. Write-only
           3. Read-only
 
  BINARY   Specifies binary file mode. In binary mode, you may read
           or write information to any byte position in the file using
           GET and PUT.
 
           In binary mode, if no ACCESS clause is present, three
           attempts are made to open the file. The attempts follow the
           same order as those for RANDOM files.
 
If mode1 is omitted, the default random-access mode is assumed.
 
The access expression specifies the operation performed on the opened
file. If the file is already opened by another process and the
specified type of access is not allowed, the OPEN fails and an error
message is generated that reads "Permission denied." The ACCESS clause
works in an OPEN statement only if you are using a version of DOS that
supports networking (DOS Versions 3.0 or later). In addition, you must
run the SHARE.EXE program (or the network startup program must run it)
to perform any locking operation. If ACCESS is used with OPEN, earlier
versions of DOS return an error message that reads "Advanced feature
unavailable."
 
The access argument can be one of the following:
 
  Access Type   Description
  READ          Opens the file for reading only.
 
  WRITE         Opens the file for writing only.
 
  READ WRITE    Opens the file for both reading and writing. This mode
                is valid only for RANDOM and BINARY files and files
                opened for APPEND.
 
The lock clause works in a multiprocessing environment to restrict
access by other processes to an open file. The lock types are as
follows:
 
  Lock Type         Description
  default           If locktype is not specified, the file may be
                    opened for reading and writing any number of times
                    by this process, but other processes are denied
                    access to the file while it is opened.
 
  SHARED            Any process on any machine may read from or write
                    to this file. Do not confuse the SHARED lock type
                    with the SHARED statement or the SHARED attribute
                    appearing in other statements.
 
  LOCK READ         No other process is granted read access to this
                    file. This access is granted only if no other
                    process has a previous READ access to the file.
 
  LOCK WRITE        No other process is granted write access to this
                    file. This lock is granted only if no other
                    process has a previous WRITE access to the file.
 
  LOCK READ WRITE   No other process is granted either read or write
                    access to this file. This access is granted only
                    if READ or WRITE access has not already been
                    granted to another process, or if a LOCK READ or
                    LOCK WRITE is not already in place.
 
 
When the OPEN is restricted by a previous process, it generates
error 70, "Permission denied," under DOS.
 
The filenum (file number) argument is an integer expression whose value
is between 1 and 255. When an OPEN is executed, the file number is
associated with the file as long as it is open. Other I/O statements
may use the number to refer to the file.
 
The reclen (record length) argument is an integer expression that, if
included, sets the record length (number of characters in one record)
for random-access files. For sequential files, the default length for
records is 512 bytes; for random-access files, the default is 128 bytes.
The value of reclen cannot exceed 32,767 bytes. If the file mode is
binary, then the LEN clause is ignored.
 
For sequential files, reclen need not correspond to an individual record
size, since a sequential file may have records of different sizes. When
used to open a sequential file, reclen specifies the number of characters
to be loaded into the buffer before the buffer is written to, or read from,
the disk. A larger buffer means more room taken from BASIC, but faster file
I/O. A smaller buffer means more room in memory for BASIC, but slower I/O.
The default buffer size is 512 bytes.
 
Syntax 2
 
  OPEN mode2,[#]filenum,file[,reclen]
 
In the second form of the OPEN syntax, mode2 is a string expression the
first character of which must be one of the following:
 
  Mode   Description
  O      Specifies sequential output mode.
  I      Specifies sequential input mode.
  R      Specifies random-access file input/output mode.
  B      Specifies binary file mode.
  A      Specifies sequential output mode and sets the file pointer
         to the end of the file and the record number to the last
         record of the file. A PRINT # or WRITE # statement extends
         (appends to) the file.
 
  Note: The second OPEN syntax does not support any of the access and
        file-sharing options found in the first syntax and is supported
        for compatibility with programs written in earlier versions of
        BASIC.
 
The following devices are supported by BASIC and can be named and
opened with the file argument:
 
  KYBD:, SCRN:, COMn:, LPTn:, CONS:.
 
The BASIC file I/O system allows you to take advantage of user-installed
devices. (See your DOS manual for information on character devices.)
 
Character devices are opened and used in the same manner as disk files.
However, characters are not buffered by BASIC as they are for disk files.
The record length for the device files is set to one.
 
BASIC only sends a carriage return at the end of a line. If the device
requires a line feed, the driver must provide it. When writing device
drivers, keep in mind that other BASIC users will want to read and
write control information. Writing and reading of device-control data
is handled by the IOCTL statement and IOCTL$ function.
 
None of the BASIC devices directly supports binary mode. However, the line
printer devices (LPT1:, LPT2:) can be opened in binary mode by adding the
BIN keyword:
 
  OPEN "LPT1:BIN" FOR OUTPUT AS #1
 
Opening a printer in BIN mode eliminates printing a carriage return at
the end of a line.
 
  Note: In INPUT, RANDOM, and BINARY modes you can open a file under a
        different file number without first closing the file. In OUTPUT
        or APPEND mode you must close a file before opening it with a
        different file number.