Expressió

Obre un canal de dades.

Sintaxi:

Open Statement diagram

Diagrama de fragments d'accés

locking fragment diagram

Open pathname For mode [Access io] [locking] As [#]filenum [Len=recLen]

Paràmetres:

pathname: Path and name of the file to open. If you try to read a file that does not exist (Access = Read), an error message appears. If you try to write to a file that does not exist (Access = Write), a new file is created.

mode: Keyword that specifies the file mode. Valid values: Append (append to sequential file), Binary (data can be accessed by bytes using Get and Put), Input (opens data channel for reading), Output (opens data channel for writing), and Random (edits relative files).

io Paraula clau que defineix el tipus d'accés. Valors vàlids Llegeix (només lectura) Escriu (només escriptura) Llegeix (ambdós).

locking: Keyword that defines the security status of a file after opening. Valid values: Shared (file may be opened by other applications), Lock Read (file is protected against reading), Lock Write (file is protected against writing), Lock Read Write (denies file access).

filenum Qualsevol expressió d'enter de 0 a 511 per indicar el nombre d'un canal de dades gratuït. A continuació podeu passar ordres a través del canal de dades per accedir al fitxer. El número de fitxer ha de ser determinat per la funció FreeFile immediatament abans de l'expressió Open.

recLen pels fitxers d'accés aleatori estableix la longitud dels registres.

note

Podeu modificar el contingut d'un fitxer que es va obrir amb l'expressió Open. Si intenteu obrir un fitxer que ja està obert, apareix un missatge d'error.


Exemple:

Sub ExampleWorkWithAFile
    Dim iNumber As Integer
    Dim sLine As String
    Dim aFile As String
    Dim sMsg As String
    aFile = "~/data.txt"
    iNumber = Freefile
    Open aFile For Output As #iNumber
    Print #iNumber, "This is a line of text"
    Print #iNumber, "This is another line of text"
    Close #iNumber
    iNumber = Freefile
    Open aFile For Input As iNumber
    While Not eof(iNumber)
        Line Input #iNumber, sLine
        If sLine <>"" Then
            sMsg = sMsg & sLine & chr(13)
        End If
    Wend
    Close #iNumber
    MsgBox sMsg
End Sub
note

If the Open statement tries to open a file to which the current user does not have read/write permissions, an I/O error will be raised.


Ens cal la vostra ajuda!