Manipulate a Binary File

Decimal BASIC allows us to read a file byte-by-byte using CHARACTER INPUT statements.
We write
OPTION CHARACTER BYTE
at the beginning of the program so as to enable to deal with a byte as a character.
Note that a character may not be a byte if the OPTION statement above is missed out, since ordinarily Decimal BASIC deals with double-byte characters.
We write a byte or bytes using PRINT statements followed by semicolons as follows.
PRINT s$;

Example 1. A program that copies a file.

100 OPTION CHARACTER BYTE
110 LET infile$="C:\BASICw32\TUTORIAL.PDF"
120 LET outfile$="C:\BASICw32\COPY-TUTORIAL.PDF"
130 OPEN #1:NAME infile$
140 OPEN #2:NAME outfile$ 
150 ERASE #2
160 DO
170    CHARACTER INPUT #1,IF MISSING THEN EXIT DO: s$
180    PRINT #2:s$;
190 LOOP
200 CLOSE #1
210 CLOSE #2
220 END

Read a whole file

If we have enough memory where the whole file can be read, we can deal with it in a string variable.
Example 2.

100 OPTION CHARACTER BYTE
110 LET infile$="C:\BASICw32\TUTORIAL.PDF"
120 LET outfile$="C:\BASICw32\COPY-TUTORIAL.PDF"
130 OPEN #1:NAME infile$
140 ASK #1: FILESIZE n
150 LET s$=""
160 FOR i=1 TO n
170    CHARACTER INPUT #1: s$(i:i)
180 NEXT i
190 CLOSE #1
200 ! Ordinarily, something are done here
210 OPEN #2:NAME outfile$ 
220 ERASE #2
230 PRINT #2:s$;
240 CLOSE #2
250 END

The above program only makes a copy, we can do various transaction adding lines between line 190 and line 210.
(Supplement)
The above program can be a bit accelerated by substituting the line 150 above with the following,since substitution of substring that does not change the byte length of the string has been accelerated from the version 5.8.8.

150 LET s$=REPEAT$(chr$(0),n)



Back