Skip to content

OPEN

Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgOpen Last revised: 2018-03-12

Opens a disk file for reading or writing using file operations.

Syntax

Open filename For Input  [Encoding "type"] [Lock {Shared|Read|Write}] As [#]filenumber
Open filename For Output [Encoding "type"] [Lock {Shared|Read|Write}] As [#]filenumber
Open filename For Append [Encoding "type"] [Lock {Shared|Read|Write}] As [#]filenumber
Open filename For Binary [Access {Read|Write}] [Lock {Shared|Read|Write}] As [#]filenumber
Open filename For Random [Access {Read|Write}] [Lock {Shared|Read|Write}] As [#]filenumber [Len = record_length]

Function style:

result = Open( filename For {Input|Output|Append} As filenumber )
result = Open( filename For Binary Access {Read|Write} As filenumber )
result = Open( filename For Random Access {Read|Write} As filenumber [Len = record_length] )

Parameters

  • filename — Name of the disk file to open. Relative paths are relative to the current directory.
  • encoding_type"ascii" (default), "utf8", "utf16", or "utf32".
  • access_typeAccess [Read] [Write] (both by default).
  • lock_typeShared or Lock [Read] [Write].
  • filenumber — An available file number (use FreeFile to get one).
  • record_length — Size in bytes of each record (Random mode, default 128).

Return Value

In function-style usage, returns a 32-bit Long: zero (0) on success, non-zero error code otherwise.

Description

Opens a disk file for reading and/or writing.

Sequential modes (Input, Output, Append):

  • Input — Read only. File must exist.
  • Output — Write only. Existing file contents are deleted.
  • Append — Write only. Writing occurs at end of file.

Random-access modes (Binary, Random):

  • Binary — Read/write of raw bytes using Get # / Put #.
  • Random — Like Binary but uses fixed-size records (Len = record_length).

Lock types control access from other processes.

Examples

Example 1: Writing to a binary file

vb
Dim buffer As String, f As Integer
buffer = "Hello World within a file."
f = FreeFile
Open "file.ext" For Binary As #f
If Err > 0 Then Print "Error opening the file" : End
Put #f, , buffer
Close
End

Example 2: Opening a COM port

vb
Open Com "COM1:9600,N,8,1" As #1
If Err > 0 Then Print "The port could not be opened."

Example 3: Function version with error checking

vb
If Open("file.ext" For Binary Access Read As #1) = 0 Then
    Print "Successfully opened file"
    Close #1
Else
    Print "Error opening file"
End If

Platform Differences

  • Linux: Filename case must match. Path separator is /.
  • Windows/DOS: Case insensitive. Path separator is \ (Windows also allows /).
  • Windows: File numbers in a DLL are not the same as in the main executable.
  • Linux: Opening a directory will succeed.

Differences from QB

  • MS-DOS device names (e.g., "LPT:") only supported in -lang qb dialect.
  • Open can be called as a function returning an error code.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft