From the help file:
<TABLE id=topTable width="100%"><TBODY><TR id=headerTableRow1><TD align=left></TD></TR><TR id=headerTableRow2><TD align=left>
Open Statement</TD></TR></TBODY></TABLE>
Enables input/output (I/O) to a file.
Syntax
Open pathname For mode [
Access access] [
lock]
As [
#]
filenumber [
Len=
reclength]
The
Open statement syntax has these parts:
<TABLE><TBODY><TR><TH>Part</TH><TH>Description</TH></TR><TR><TD>
pathname</TD><TD>Required.
String expression that specifies a file name — may include directory or folder, and drive.</TD></TR><TR><TD>
mode</TD><TD>Required.
Keyword specifying the file mode:
Append,
Binary,
Input,
Output, or
Random. If unspecified, the file is opened for
Random access.</TD></TR><TR><TD>
access</TD><TD>Optional. Keyword specifying the operations permitted on the open file:
Read,
Write, or
Read Write.</TD></TR><TR><TD>
lock</TD><TD>Optional. Keyword specifying the operations restricted on the open file by other processes:
Shared,
Lock Read,
Lock Write, and
Lock Read Write.</TD></TR><TR><TD>
filenumber</TD><TD>Required. A valid
file number in the range 1 to 511, inclusive. Use the
FreeFile function to obtain the next available file number.</TD></TR><TR><TD>
reclength</TD><TD>Optional. Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.</TD></TR></TBODY></TABLE>
Remarks
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 and determines the mode of access to use with the buffer.
If the file specified by
pathname doesn't exist, it is created when a file is opened for
Append,
Binary,
Output, or
Random modes.
If the file is already opened by another process and the specified type of access is not allowed, the
Open operation fails and an error occurs.
The
Len clause is ignored if
mode is
Binary.
Important In
Binary,
Input, and
Random modes, you can open a file using a different file number without first closing the file. In
Append and
Output modes, you must close a file before opening it with a different file number.
Example
This example illustrates various uses of the
Open statement to enable input and output to a file.
The following code opens the file <TABLE><TBODY><TR><TD>
<CODE>TESTFILE</CODE></PRE></TD></TR></TBODY></TABLE>
in sequential-input mode.
<TABLE><TBODY><TR><TD>
<CODE>
Open "TESTFILE"
For Input As #1' Close before reopening in another mode.Close #1</CODE></PRE></TD></TR></TBODY></TABLE>
This example opens the file in Binary mode for writing operations only.
<TABLE><TBODY><TR><TD>
<CODE>
Open "TESTFILE"
For Binary Access Write As #1' Close before reopening in another mode.Close #1</CODE></PRE></TD></TR></TBODY></TABLE>
The following example opens the file in Random mode. The file contains records of the user-defined type <TABLE><TBODY><TR><TD>
<CODE>Record</CODE></PRE></TD></TR></TBODY></TABLE>
.
<TABLE><TBODY><TR><TD>
<CODE>Type Record ' Define user-defined type. ID As Integer Name As String * 20End TypeDim MyRecord As Record ' Declare variable.
Open "TESTFILE"
For Random As #1
Len = Len(MyRecord)' Close before reopening in another mode.Close #1</CODE></PRE></TD></TR></TBODY></TABLE>
This code example opens the file for sequential output; any process can read or write to file.
<TABLE><TBODY><TR><TD>
<CODE>
Open "TESTFILE"
For Output Shared As #1' Close before reopening in another mode.Close #1</CODE></PRE></TD></TR></TBODY></TABLE>
This code example opens the file in Binary mode for reading; other processes can't read file.
<TABLE><TBODY><TR><TD>
<CODE>
Open "TESTFILE"
For Binary Access Read Lock Read As #1</CODE></PRE></TD></TR></TBODY></TABLE>