'1. opening the file in binary mode,
'2. writing a few hex FFs to it,
'3. then reading one byte back in ,
'4. modifying it and writing it back into the file at the same location?
'So, how big is maximal value for writing to file? Is it integer value? Is it 0 to 256, so _UNSIGNED BYTE (1 byte record)? or
' is it 0 to 65535, so _UNSIGNED INTEGER (2 byte record), or
' is it 0 to 4294967295, _UNSIGNED LONG (4 byte record), or
' is it 0 to 18446744073709551615, _UNSIGNED INTEGER64 (8 byte record)
'
' OR SIGNED integer value? -128 to 127 _BYTE, (1 byte record)
' -32768 to 32767 INTEGER (2 byte record)
' -2147483648 to 2147483647 LONG (4 byte record)
' -9223372036854775808 to 9223372036854775807 INTEGER64 (8 byte record)
' OR DECIMAL value? use SINGLE type up to 7 digits (4 byte lenght record), can not be _UNSIGNED
' or use DOUBLE type up ti 15 digits (8 byte lenght record), can not be _UNSIGNED.
'so for code:
'1]
File$ = "BinaryFile" 'new binary file name
channel
= FREEFILE 'give free number for opening fileIF UCASE$(choice$
) = "Y" THEN KILL File$
'if file exist and you set OWERWRITE, delete old file with the same name
'write 10 _UNSIGNED _BYTE values to file. Are possible TWO ways how do it. First is convert every value to _UNSIGNED _BYTE numeric type using _MK$, or
'with DIM:
'2]
DIM Value
(9) AS _UNSIGNED _BYTE 'create 10 records in array Value, type _UNSIGBNED _BYTE in memory PRINT "This values are now writed to binary file "; File$
Value(fill) = 10 * fill 'write this values to array: Value(0) = 0, Value(1) = 10 ..... Value(9) = 90
PUT #channel
, , Value
(fill
) ' write it to file
'3]
s:
INPUT "Which record read from file? [0-9]"; rec
'beacuse all values are _UNSIGNED _BYTE type, is every record lenght 1 byte.
'reading value from file (file is opened)
PRINT "Value nr. "; rec;
"writed in file is "; ValueNr
INPUT "Read next value? [Y/N]"; rnv$
'4] Modifiyng it and write to the same position:
'is as reading file:
t:
INPUT "Input record number (0-9) for rewriting"; recnr
PRINT "Insert new value in range 0 - 255, OR insert bigger value, but this":
PRINT "is then writed BAD!, because is OUT OF RANGE for _UNSIGNED _BYTE type!" INPUT "Insert new value for this position"; NewVal
'again: 1 record lenght is 1 byte. So first shift to area i n file contains previous record:
'two ways how do it:
SEEK #channel
, recnr
+ 1 ' because record 0 start on byte 1 and not on byte 0. SEEK shift position in file
'or in one step: PUT #channel, recnr+1 , newval
'now read and print all values from file to screen:
PRINT "In file "; File$;
"are writed this values:" 'i use previous _UNSIGNED _BYTE type variable named as ValueNr
GET #channel
, read_it
, ValueNr