Skip to content
Paul Squires edited this page Oct 19, 2026 · 2 revisions

Home › File I/O Functions › Binary

Binary

Opens a file with Open for reading and writing bytes at any position.

Syntax

Open filename for Binary [Access access_type] [Lock lock_type] as [#]filenum

Parameters

  • filename
  • access_type
  • lock_type
  • filenum

Description

Opens a file or device for reading and/or writing binary data in the file filenum, with free format.
If the file does not exist, a new file will be created. The file pointer is initialized by Open at byte no. 1.
Get # and Put # file operations move the file pointer according to the size of the data, the pointer can be set to any byte in the file.
The data existing in the file is preserved by Open.
This file mode can use any buffer variable to read/write data in the file.
The data is saved in binary mode, in the same internal format FreeBASIC uses, by means of Get # and Put #.

filename must be a string expression resulting in a legal file name in the target OS, without wildcards. The file will be sought for in the present directory, unless a path is given.

Access_type By default Binary mode allows to both read and write the file, unless an Access type is specified, it must be one of:

Lock_type indicates the way the file is locked for other processes (users or threads), it is one of:

If no lock type is stated, the file will be Shared for other threads of the program and Lock Read Write for other programs.
Lock and Unlock can be used to restrict temporally access to parts of a file.

filenum is a valid file number (in the range 1..255) not being used for any other file presently open. The file number identifies the file for the rest of file operations. A free file number can be found using the FreeFile function.

Remarks

Differences from QB

  • None

See also

Examples

'' Create a binary data file with one number in it

Dim x As Single = 17.164

Open "MyFile.Dat" For Binary As #1

  '' put without a position setting will put from the last known file position

  '' in this case, the very beginning of the file.

  Put #1, , x

Close #1
'' Now read the number from the file

Dim x As Single = 0

Open "MyFile.Dat" For Binary As #1

  Get #1, , x

Close #1

Print x
'' Read entire contents of a file to a string

Dim txt As String

Open "myfile.txt" For Binary Access Read As #1

  If LOF(1) > 0 Then

    '' our string has as many characters as the file has in bytes

    txt = String(LOF(1), 0)

    '' size of txt is known.  entire string filled with file data

    Get #1, , txt

  End If

Close #1

Print txt

Category: File I/O Functions
Kind: keyword


Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.

Clone this wiki locally