Specifies file or device to be opened for binary mode

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

file name to open
indicates whether the file may be read from, written to or both
locking to be used while the file is open
unused file number to associate with the open file

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:
  • Read - the file is opened for input only
  • Write - the file is opened for output only
  • Read Write - the file is opened for input and output (the default)

Lock_type indicates the way the file is locked for other processes (users or threads), it is one of:
  • Shared - The file can be freely accessed by other processes
  • Lock Read - The file can't be opened simultaneously for reading
  • Lock Write - The file can't be opened simultaneously for writing
  • Lock Read Write - The file cannot be opened simultaneously by other processes.
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.

'' 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

Differences from QB:
See also:
Back to File I/O Functions
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki phatcode