Opening Files - Input/Output

VB 6.0


Syntax:

Open "strFileName" [For Mode] [AccessRestriction]

[LookType] As [#]intFileNum [Len = intRecord/reclength]

Note:

Open "aFile.txt" as #1

The above opens a file named aFile.txt in Ranom Mode with the file handler of 1




  • Open

    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.

    The Open statement associates a number to a handle also called a channel. Once the association exist, the program uses the number for file access of both input and output: I/O is depended on what mode value is used (see [For Mode]).



  • "strFileName"

    Required PathName String expression that specifies a file name. It may include directory or folder, and drive.

    The strFileName (PathName) is a file that is opened to be used for input/output from/to a disk drive. Once it is Open, the program refers to it via it's file handler number, and not it's name.

    If the file specified by the pathname (strFileName) doesn't exist, it is created when a file is opened for Append, Binary, Output, or Random modes.



  • [For Mode]

    Mode is a special keyword, shown in the chart below, that indicates the files access mode. If unspecified, the file is opened for Random access.

    When you open a sequential file, you can't change its data. You can either read it (and store it to another file) or overwrite the entire file with the new data. To do so, you must open the file for Input, read its data, and then close the file. To overwrite it, open it again (this time for output) and save the new data to it.

    Use Append if you don't want to overwrite an existing file but want to add to the end of it.

    Keyword (Mode) Description
    Append Sequential File Type - Opens a file number to a file for sequential output, beginning at the end of the file if the file exists. If the file doesn't exist, VB creates the file. Append never overwrites existing file data.
    Binary Binary File Type - Opens a file number to a file for binary data access. In Binary mode, you can access a file at the byte level, meaning that you can write and read individual bytes to and from the file. The Len clause is ignored if mode is Binary.
    Input Sequential File Type - Opens a file number to a file for sequential input, starting at the beginning of the file. Data is read in the same order it was sent out the file.

    Read - Syntax:

    Input # intFileNumber, Variable1] _
        [, Variable2][, VariableN]

    Output Sequential File Type - Opens a file number to a file for sequential output, starting at the beginning of the file. If the file doesn't exist when you issue the Open statement, Visual Basic creates the file, otherwise, VB overwrites the file.
    Write / Print - Syntax:

    Write # intFileNumber, Variable1] _
        [, Variable2][, VariableN]

    Print # intFileNumber, Variable1] _
        [, Variable2][, VariableN]

    Random Random Access File Type - Opens a file number to a file for random read and write access. This mode allows data to be read from and writen to a file at any specific record boundary.
    Reads a file

    Get [#]intFileNumber, [intRecNum,] variable

    Writes a file

    Put [#]intFileNumber, [intRecNum,] variable



  • [AccessRestriction]

    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.

    Read Read-Only access lets users see the contents of the file but leaves them unable to modify it
    Write Write access lets users modify the file
    Read Write Read Write access lets users do both read and modify the file.



  • [LockType]

    Shared Shared lets all users access the file simultaineously
    Lock Read Lock read Locks the file so that only the person with the file open for reading can access the file
    Lock Write Lock Write Locks the file so that only the person who has the file open for write access can write to the file
    Lock Read Write Lock Read Write locks the file from all users except the one who has the file open for read and write acess.


  • As [#]intFileNum

    A valid file number (handle) in the range of 1 to 511 must be assigned to open a file and reference that file.

    Locating a Free File Number with the FreeFile() Function

    The FreeFile()function can be used to determine the next available file number.

    Example:
    intFileNumber = FreeFile

    Open "AccPay.Dat" for Output as intFileNumber



  • [Len = intRecord/recLength]

    The length specified by the Len = intRecordLength is used by Random-Access files as the size of data records that will be passed from VB to the file. This size is necessary when accessing records from a file.

    The first record in a file begins at location 1, and all subsequent records are written at locations in increments of 1. The actual location in the file of any given record is N x intRecordLength, where N is the record number. The reclength is a number less than or equal to 32,767 (bytes).

    For files opened for random acccess, this value is the record length.

    For sequential files, this value is the number of characters buffered.

    The Len clause is ignored if mode is Binary.

    Note:

    Accessing records operates quite like the way you access arrays. Whereas the first element in an array is stored sections or define your arrays to begin with element 1 and ignore the 0 subscript.

    Open Statement Examples
    Opening a file in Sequential-input mode

    ' FreeFile gives next File number avalible

    PhoneDirectory = FreeFile

    Open "c:/phone/phone.dat" For Input As PhoneDirectory

    ' Close Before opening in another Mode

    Close PhoneDirectory

    Open a 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 ' FreeFile gives next File number avalible

    PhoneDirectory = FreeFile

    Dim MyRecord as Record ' Declare variable

    Open "c:/phone/phone.dat" For Random As PhoneDirectory _
         Len = Len(MyRecord)

    ' Close Before opening in another Mode

    Close PhoneDirectory

    Opens the file for sequential output ; any process can read or write to file

    ' FreeFile gives next File number avalible

    PhoneDirectory = FreeFile

    Open "c:/phone/phone.dat" For Output Shared As PhoneDirectory

    ' Close Before opening in another Mode

    Close PhoneDirectory

    Opens file in Binary mode for reading; other processes can't read file.

    ' FreeFile gives next File number avalible

    PhoneDirectory = FreeFile

    Open "c:/phone/phone.dat" For Binary Access Read _
          Lock Read As PhoneDirectory

    ' Close Before opening in another Mode

    Close PhoneDirectory

    Open a file in Binary Mode for writing operations only
    ' FreeFile gives next File number avalible

    PhoneDirectory = FreeFile

    Open "c:/phone/phone.dat" For Binary Access Write _
          As PhoneDirectory

    ' Close Before opening in another Mode

    Close PhoneDirectory



  • VB Page

    Home