BB___File_Type_Binary

=Binary Access Files= toc

Description
> With files opened for Binary access, data is written to the file or read from the file byte by byte as characters. See Chr$(n). Binary access files must be OPENed to be used and they must be CLOSEd when finished.

General
> Any file that you need access to at a per byte basis can be opened as binary, but you usually open non-text files in this mode.

> Data can be read or written beginning at any location in a Binary access file. A file pointer is used with Binary access files to locate a point in the file at which to read or write. The SEEK command sets the pointer to the desired spot in the file for reading or writing. The LOC(#handle) function will retrieve the current position of the pointer.

> Data can be read with the INPUT$ function which reads one or more characters as specified in the function parameter.

> Data is written to the file using the PRINT statement. The **E**nd-**O**f-**L**ine characters, carriage return - chr$(13) - and line feed - chr$(10), are never automatically appended to data written to the file. (again: if you PRINT to a binary file, new line will NOT be inserted, contrary to sequential file). > For this reason, binary files are not good candidates for saving text files since without end-of-line characters, all sentences will run together. To create line breaks, one must add the end-of-line characters manually.

Syntax
To OPEN a file To CLOSE a file To SEEK to file position To LOCATE current file position To WRITE a byte to the file To READ the data at the current location
 * Using Binary Access Files**
 * 1) open //fileName$// for binary as //#handle//
 * 1) close //#handle//
 * 1) seek //#handle, fpos//
 * 1) //fpos// = loc(//#handle//)
 * 1) //#handle// chr$(143) ' (Preferred form)
 * 2) print //#handle,// chr$(143) ' (Obsolete form)
 * 1) //var$// = input$( //#handle, sizeToRead//)

Hints
> LOF and EOF can be used with Binary access files to get the length of the file and to read to the end of the file.

> When working with binary files in JB, there is no command to truncate the file at a certain size. If the file will grow, it will stay that long until you delete it. And even if you do not use information after some point in a file, that information will still be there until you delete the file.

> You cannot open files for binary that are read-only. For instance, files located on a CD-ROM.

> For comparative purposes, note that files opened for Sequential access are read up to a comma delimiter or as a full line, and reading always starts at the beginning of the file. Random files are normally read as records, one full record at a time, and from any location in the file. Binary access files are read as a series of characters at a time, from any location in the file. > **DO NOT**use >> # input //#handle, txt$// > on binary files - it will read file as sequential, up to a comma delimiter or new line, that doesn't make sense for binary files.

> You usually don't open text files for binary, since it is easier to handle them sequentially. In fact, most binary files are program executables or image files, such as bitmaps and GIFs, and other file types in which the data is interpreted differently than ordinary textual characters. This example shows how the loc and seek commands work, using text data that is more readable for humans.

include component="page" page="BB___File_Type_Table" editable="1" wrap="1"

Example
code format="lb" 'binary file example

'truncate file (make it empty) first 'KILL would error if file not exist, but this will make sure file is empty open "myfile.bin" for output as #myfile close #myfile

txt$ = "I like programming with Just BASIC." open "myfile.bin" for binary as #myfile

'write some data to the file print #myfile, txt$ print "Original data in file is: ";txt$

'retrieve the position of the file pointer (since we just wrote it, it is file length) nowPos = LOC(#myfile)

'move the file-pointer nowPos = nowPos - len("Just BASIC.")   'move it a few words back seek #myfile, nowPos

'read the data at the current location nBytes=6 txt$=input$(#myfile, nBytes)

'print txt$ in mainwin print nBytes; " bytes of data at ";nowPos;" is: ";txt$

'move the file-pointer seek #myfile, 2

'get current file pointer position OldLOC = loc(#myfile)

'write some data to the middle of the file print #myfile, "love"

'get new file pointer position NewLOC = loc(#myfile)

'display the number of bytes written print NewLOC - OldLOC; " bytes written at ";OldLOC

'move the file pointer to the beginning seek #myfile, 0

'read the data - whole file txt$ = input$( #myfile, lof( #myfile))

'print data in mainwin print "New Data is: ";txt$

'close the file close #myfile end code

Useful Procedures
code format="lb" Place a useful function using this keyword here code