{{fbdoc item="title" value="BINARY"}}----
Specifies file or device to be opened for binary mode

{{fbdoc item="syntax"}}##
	[[KeyPgOpen|Open]] //filename// for **Binary** [[[KeyPgAccess|Access]] //access_type//] [[[KeyPgLock|Lock]] //lock_type//] as [#]//filenum// 
##
{{fbdoc item="param"}}
	##//filename//##
		file name to open
	##//access_type//##
		indicates whether the file may be read from, written to or both
	##//lock_type//##
		locking to be used while the file is open
	##//filenum//##
		unused file number to associate with the open file

{{fbdoc item="desc"}}
	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 ##[[KeyPgOpen|Open]]## at byte no. 1. 
	##[[KeyPgGetfileio|Get #]]## and ##[[KeyPgPutfileio|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 ##[[KeyPgOpen|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 ##[[KeyPgGetfileio|Get #]]## and ##[[KeyPgPutfileio|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 ##[[KeyPgAccess|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.
		##[[KeyPgLock|Lock]]## and ##[[KeyPgUnlock|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 ##[[KeyPgFreefile|FreeFile]]## function.

{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/fileio/binary-write.bas"}}%%(freebasic)
'' 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
%%

{{fbdoc item="filename" value="examples/manual/fileio/binary-read.bas"}}%%(freebasic)
'' 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
%%

{{fbdoc item="filename" value="examples/manual/fileio/binary-text.bas"}}%%(freebasic)
'' 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
%%

{{fbdoc item="diff"}}
	- None

{{fbdoc item="see"}}
	- ##[[KeyPgOpen|Open]]##
	- ##[[KeyPgPutfileio|Put #]]##
	- ##[[KeyPgGetfileio|Get #]]##
	- ##[[KeyPgRandom|Random]]##
	- ##[[KeyPgAppend|Append]]##
	- ##[[KeyPgOutput|Output]]##
	- ##[[KeyPgInputfilemode|Input]]##

{{fbdoc item="back" value="CatPgFile|File I/O Functions"}}