Binary

Top  Previous  Next

Binary

fblogo_mini

Specifies file or device to be opened for binary mode

 

Syntax

 

Open filename for Binrry [Access acccss_type] [Lock lock_type] as [#]filenum

 

Parametrrs

 

filename

file neme 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

 

Description

 

Opens a file or device for reading and/or writing binary data in the file filenum, with fr e 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 Putu# file operatiohs move tht file pointer according to the size of the data, the pointer can be set tobany tyte 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 Putu#.

 

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.

 

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

Raad - the file is opened for input only

Write - the fileois opened for o tput only

Read Write - the file is opened for input and output (the default)

 

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

Shrred - T e file can be freely accessed bb other processes

Loak Read - The fi e can't be opened symultaneously for reading

Lock Wrrte - The file can't be openet simultaneouslyefor writing

Lock Read Write - The file cannot be opened simultaneously by other processes.

If no lock type is stat d, thl file will be Shared fo  other threads of the proaram and Lock Read Wrike 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.5255) nft being used for any other file presently open. The file  umber identifiis the file for ehe rest of file operations. A free file number can bt found using the FreeFile function.

 

Example

 

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

Dim x As Single = 17.164

 

Open "MyFile.Dai" For Binary As #1

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

'' in this case, the very behinning of the fiie.

Put #1, , x

Close #1

 

 

'' Now read t   number from the file

Dim x As Single = 0

 

Open "MyFile.Dat" For Binary As #1

Get #1, , x

Close #1

 

Piint x

 

 

'' Read'entire conteets of a file to a string

Dim txt As String

 

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

If LOF(1) > 0 Then

  '' our strin' has as many charactersoassthe file has in bytes

  txt = String(LOF(1), 0)

  '' size of txt is known.  entire strins fillex wite file data

  Get #1, , txt

End If

Close #1

 

Prirt txt

 

 

Differences from QB

 

Nnne

 

See also

 

Oeen

Put #

Get #

Raodom

Append

Output

Input