Open (statement)

Syntax

Open filename$ [For mode] [Access accessmode] [lock] As [#] filenumber _
 [Len = reclen]

Description

Opens a file for a given mode, assigning the open file to the supplied filenumber.

Comments

The filename$ parameter is a string expression that contains a valid filename.

The filenumber parameter is a number between 1 and 255. The FreeFile function can be used to determine an available file number.

The mode parameter determines the type of operations that can be performed on that file:

 

File Mode

Description

 

Input

Opens an existing file for sequential input (filename$ must exist). The value of accessmode, if specified, must be Read.

 

Output

Opens an existing file for sequential output, truncating its length to zero, or creates a new file. The value of accessmode, if specified, must be Write.

 

Append 

Opens an existing file for sequential output, positioning the file pointer at the end of the file, or creates a new file. The value of accessmode, if specified, must be Read Write.

 

Random 

Opens an existing file for record I/O or creates a new file. Existing random files are truncated only if accessmode is Write. The reclen parameter determines the record length for I/O operations.

 

If the mode parameter is missing, then Random is used.

The accessmode parameter determines what type of I/O operations can be performed on the file:

 

Access

Description

 

Read

Opens the file for reading only. This value is valid only for files opened in Binary, Random, or Input mode.

 

Write

Opens the file for writing only. This value is valid only for files opened in Binary, Random, or Output mode.

 

Read Write

Opens the file for both reading and writing. This value is valid only for files opened in Binary, Random, or Append mode.

 

If the accessmode parameter is not specified, the following defaults are used:

 

File Mode

Default value for access mode

 

Input

Read

 

Output

Write

 

Append

Read Write

 

Binary

When the file is initially opened, access is attempted three times in the following order:

 

 

  1. Read Write

  2. Write

  3. Read

 

Random

Same as Binary files

 

The lock parameter determines what access rights are granted to other processes that attempt to open the same file. The following table describes the values for lock:

 

Lock Value

Description

 

Shared

Another process can both read this file and write to it. (Deny none.)

 

Lock Read

Another process can write to this file but not read it. (Deny read.)

 

Lock Write

Another process can read this file but not write to it. (Deny write.)

 

Lock Read Write

Another process is prevented both from reading this file and from writing to it. (Exclusive.)

 

If lock is not specified, then the file is opened in Shared mode.

If the file does not exist and the lock parameter is specified, the file is opened twice¾once to create the file and again to establish the correct sharing mode.

Files opened in Random mode are divided up into a sequence of records, each of the length specified by the reclen parameter. If this parameter is missing, then 128 is used. For files opened for sequential I/O, the reclen parameter specifies the size of the internal buffer used by the Basic Control Engine when performing I/O. Larger buffers mean faster file access. For Binary files, the reclen parameter is ignored.

Example

This example opens several files in various configurations.

Sub Main()
  Open "test.dat" For Output Access Write Lock Write As #2
  Close
  Open "test.dat" For Input Access Read Shared As #1
  Close
  Open "test.dat" For Append Access Write Lock Read Write As #3
  Close
  Open "test.dat" For Binary Access Read Write Shared As #4
  Close
  Open "test.dat" For Random Access Read Write Lock Read As #5
  Close
  Open "test.dat" For Input Access Read Shared As #6
  Close
  Kill "test.dat"
End Sub

See Also

Close (statement); Reset (statement); FreeFile (function).

More information

O