Opens a file for input or output.
Public Sub FileOpen( _ ByVal FileNumber As Integer, _ ByVal FileName As String, _ ByVal Mode As OpenMode, _ Optional ByVal Access As OpenAccess = OpenAccess.Default, _ Optional ByVal Share As OpenShare = OpenShare.Default, _ Optional ByVal RecordLength As Integer = -1 _ )
Exception type | Error number | Condition |
---|---|---|
5 | Invalid Access, Share, or Mode. | |
5 | WriteOnly file is opened for Input. | |
5 | ReadOnly file is opened for Output. | |
5 | ReadOnly file is opened for Append. | |
5 | Record length is negative (and not equal to -1). | |
52 | FileNumber is invalid (<-1 or >255), or FileNumber is already in use. | |
55 | FileName is already open, or FileName is invalid. |
You must open a file before any I/O operation can be performed on it. FileOpen 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 FileName doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.
The channel to open can be found using the FreeFile() function.
This example illustrates various uses of the FileOpen function to enable input and output to a file.
The following code opens the file TESTFILE
in Input mode.
FileOpen(
1, "TESTFILE", OpenMode.Input)
' Close before reopening in another mode. FileClose(1)
This example opens the file in Binary mode for writing operations only.
FileOpen(
1, "TESTFILE", OpenMode.Binary,OpenAccess.Write)
' Close before reopening in another mode. FileClose(1)
The following example opens the file in Random mode. The file contains records of the structure Person
.
Structure Person
<VBFixedString(30)> Dim Name As String
Dim ID As Integer
End Structure
' Count 30 for the string, plus 4 for the integer.
FileOpen(
1, "TESTFILE", OpenMode.Random, , , 34)
' Close before reopening in another mode.
FileClose(1)
This code example opens the file in Output mode; any process can read or write to file.
FileOpen(
1, "TESTFILE", OpenMode.Output, OpenShare.Shared)
' Close before reopening in another mode. FileClose(1)
This code example opens the file in Binary mode for reading; other processes can't read file.
FileOpen(
1, "TESTFILE", OpenMode.Binary, OpenAccess.Read, _ OpenShare.LockRead)
FileClose Function | FreeFile Function |