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 the following parameters:
filename
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 argument mode 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 argument
access 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.
|
Example
The following example writes data to a test file and reads it back.
Sub Main()
Open "TESTFILE" For Output As #1 ' Open to write file.
userData1$ = InputBox("Enter your own text here")
userData2$ = InputBox("Enter more of your own text here")
Write #1, "This is a test of the Write # statement."
Write #1,userData1$, userData2
Close #1
Open "TESTFILE" for Input As #2 ' Open to read file.
Do While Not EOF(2)
Line Input #2, FileData ' Read a line of data.
PRint FileData ' Construct message.
Loop
Close #2 ' Close all open files.
MsgBox "Testing Print Statement" ' Display message.
Kill "TESTFILE" ' Remove file from disk.
End Sub