Open Statement

  1. Open filename$ [For mode] [Access access] As [#]filenumber

Opens a file for input and output operations.

You must open a file before any I/O operation can be performed on it.

The Open statement has these parts:

Part

Description

file

File name or path.

mode

Reserved word that specifies the file mode: Append, Binary Input, Output

Access

Reserved word that specifies which operations are permitted on the open file: Read, Write.

filenumber

Integer expression with a value between 1 and 255, inclusive. When an Open statement is executed, filenumber is associated with the file as long as it is open. Other I/O statements can use the number to refer to the file.

If file doesn't exist, it is created when a file is opened for Append, Binary or Output modes.

The mode argument is a reserved word that specifies one of the following file modes.

Mode

Description

Input

Sequential input mode.

Output

Sequential output mode.

Append

Sequential output mode. Append sets the file pointer to the end of the file. A Print # or Write # statement then extends (appends to) the file.

The access argument is a reserved word that specifies the operations that can be 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 operation fails, and a Permission denied error occurs. The Access clause works only if you are using a version of MS-DOS that supports networking (MS-DOS version 3.1 or later). If you use the Access clause with a version of MS-DOS that doesn't support networking, a feature unavailable error occurs. The argument access can be one of the following reserved words.

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 mode.

The following example writes data to a test file and reads it back.

Related topics:

Get statement, Put statement

More:

Example - Open Statement