Table of contents
TOC
折叠目录
展开目录

Open 语句Open Statement

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

启用对文件的输入/输出 (I/O)。Enables input/output (I/O) to a file.

语法Syntax

打开路径名对于模式[访问访问][]作为[ # ] filenumber [ Len = reclength ]OpenpathnameFormode [ Accessaccess ] [ lock ] As [ # ] filenumber [ Len = reclength ]

打开的语句语法具有下列组成部分:The Open statement syntax has these parts:

部件Part说明Description
路径名pathname必填。字符串表达式,指定文件名 — — 可能包括目录或文件夹和驱动器。Required. String expression that specifies a file name — may include directory or folder, and drive.
modemode必填。关键字指定文件模式:追加二进制输入输出随机。如果未指定,则为随机访问打开文件。Required. Keyword specifying the file mode: Append, Binary, Input, Output, or Random. If unspecified, the file is opened for Random access.
访问access可选。打开的文件的关键字指定操作允许:读取写入读写Optional. Keyword specifying the operations permitted on the open file: Read, Write, or Read Write.
锁定lock可选。关键字指定打开的文件受到其他进程的操作:共享锁定读取写入锁锁读写Optional. Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
filenumberfilenumber必填。有效文件号在 1 到 511,包容的范围。使用函数来获得下一个可用的文件号。Required. A valid file number in the range 1 to 511, inclusive. Use the FreeFile function to obtain the next available file number.
reclengthreclength可选。小于或等于 32,767(字节)的数。对于以随机访问模式打开的文件,此值为记录长度。对于序列文件,此值为缓冲的字符数。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.

备注可以在其上执行任何 I/O 操作前,必须打开一个文件。 打开的文件 i/o 分配一个缓冲区,并确定要使用的缓冲区的访问模式。如果指定_的路径名_的文件不存在,创建时追加二进制输出随机模式打开的文件。如果该文件已由另一个进程,不允许访问指定的类型打开操作将会失败并发生错误。如果_模式_是二进制Len子句将被忽略。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

此示例演示使用Open语句来实现输入和输出到一个文件。This example illustrates various uses of the Open statement to enable input and output to a file.

下面的代码在连续输入模式下打开文件。The following code opens the file in sequential-input mode.

Open "TESTFILE" For InputAs#1 
' Close before reopening in another mode. 
Close #1 

此示例在二进制 (Binary) 模式下打开文件,仅供写操作。This example opens the file in Binary mode for writing operations only.

Open "TESTFILE" For Binary Access Write As #1 
' Close before reopening in another mode. 
Close #1 

下面的示例在随机 (Random) 模式下打开文件。文件中包含用户定义的类型的记录。The following example opens the file in Random mode. The file contains records of the user-defined type .

Type Record ' Define user-defined type. 
 ID As Integer 
 Name As String * 20 
End Type 

Dim MyRecord As Record ' Declare variable. 
Open "TESTFILE" For Random As #1 Len = Len(MyRecord) 
' Close before reopening in another mode. 
Close #1 

此代码示例打开文件供顺序输出;任何进程均可以读取或写入该文件。This code example opens the file for sequential output; any process can read or write to file.

Open "TESTFILE" For Output Shared As #1 
' Close before reopening in another mode. 
Close #1 

此代码示例以二进制 (Binary) 模式打开文件供读取;其他进程不能读取文件。This code example opens the file in Binary mode for reading; other processes can't read file.

Open "TESTFILE" For Binary Access Read Lock Read As #1 
© 2018 Microsoft