Author Topic: Binary file mode questions  (Read 2881 times)

0 Members and 1 Guest are viewing this topic.

Offline Videolabguy

  • Newbie
  • Posts: 3
  • Squirrel?!
    • View Profile
Binary file mode questions
« on: April 23, 2019, 11:42:59 pm »
I want to create a binary file that is byte based. Capable of accessing individual bytes, reading them in, logically modifying the bits within, and then writing the new byte back to the file. I did this many many years ago in QB4.5 no problem. This is for making state machine patterns I burn into flash memory and then clock out with a simple 19 bit counter. Think: "Player piano roll".
The QB keywords I can't recall were the ones that control the file pointer, read and write binary.
I can create the file. My counters is going from 0 to 524,287 (half a mega byte) But the resulting file is zero bytes long.

Would someone be kind enough to create a very simple code example of:
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?


Thanks in advance!
~Videolabguy~

FellippeHeitor

  • Guest
Re: Binary file mode questions
« Reply #1 on: April 23, 2019, 11:58:23 pm »
Here's to get you started:

1. opening the file in binary mode,
http://www.qb64.org/wiki/OPEN

2. writing a few hex FFs to it,
http://www.qb64.org/wiki/PUT

3. then reading one byte back in ,
http://www.qb64.org/wiki/SEEK_(statement)
http://www.qb64.org/wiki/GET

4. modifying it and writing it back into the file at the same location?
http://www.qb64.org/wiki/PUT

Welcome to the forum.
« Last Edit: April 24, 2019, 12:13:11 am by FellippeHeitor »

Offline Videolabguy

  • Newbie
  • Posts: 3
  • Squirrel?!
    • View Profile
Re: Binary file mode questions
« Reply #2 on: April 24, 2019, 12:17:03 am »
Thanks!

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Binary file mode questions
« Reply #3 on: April 24, 2019, 07:42:17 am »
Small training... :-D

Code: QB64: [Select]
  1.  
  2. '1. opening the file in binary mode,
  3. '2. writing a few hex FFs to it,
  4. '3. then reading one byte back in ,
  5. '4. modifying it and writing it back into the file at the same location?
  6.  
  7.  
  8. '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
  9. '                                                                       is it 0 to 65535, so               _UNSIGNED INTEGER (2 byte record), or
  10. '                                                                       is it 0 to 4294967295,             _UNSIGNED LONG (4 byte record), or
  11. '                                                                       is it  0 to 18446744073709551615,  _UNSIGNED INTEGER64 (8 byte record)
  12. '
  13. '                                           OR SIGNED integer value?             -128 to 127                   _BYTE, (1 byte record)
  14. '                                                                               -32768 to 32767                 INTEGER (2 byte record)
  15. '                                                                          -2147483648 to 2147483647            LONG (4 byte record)
  16. '                                                                 -9223372036854775808 to 9223372036854775807   INTEGER64 (8 byte record)
  17.  
  18. '                                           OR DECIMAL value?  use SINGLE type up to 7 digits (4 byte lenght record), can not be _UNSIGNED
  19. '                                                          or  use DOUBLE type up ti 15 digits (8 byte lenght record), can not be _UNSIGNED.
  20.  
  21. 'so for code:
  22. '1]
  23. File$ = "BinaryFile" 'new binary file name
  24. channel = FREEFILE 'give free number for opening file
  25. IF _FILEEXISTS(File$) THEN PRINT File$; " exists. Owervrite it? [Y/N]": INPUT choice$ 'test, if file already exists or not
  26. IF UCASE$(choice$) = "Y" THEN KILL File$ 'if file exist and you set OWERWRITE, delete old file with the same name
  27.  
  28. OPEN "BinaryFile" FOR BINARY AS #channel 'create new binary file
  29. '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
  30. 'with DIM:
  31.  
  32.  
  33. '2]
  34. DIM Value(9) AS _UNSIGNED _BYTE 'create 10 records in array Value, type _UNSIGBNED _BYTE in memory
  35. PRINT "This values are now writed to binary file "; File$
  36. FOR fill = 0 TO 9
  37.     Value(fill) = 10 * fill 'write this values to array: Value(0) = 0, Value(1) = 10 ..... Value(9) = 90
  38.     PRINT Value(fill);: IF fill < 9 THEN PRINT LTRIM$(","); ELSE PRINT
  39.     PUT #channel, , Value(fill) '  write it to file
  40. NEXT fill
  41.  
  42. '3]
  43. s:
  44. INPUT "Which record read from file? [0-9]"; rec
  45. 'beacuse all values are _UNSIGNED _BYTE type, is every record lenght 1 byte.
  46. IF rec > 9 THEN PRINT "invalid value": GOTO s
  47.  
  48. 'reading value from file (file is opened)
  49.  
  50. SEEK #channel, rec + 1
  51. GET #channel, , ValueNr
  52. PRINT "Value nr. "; rec; "writed in file is "; ValueNr
  53.  
  54. INPUT "Read next value? [Y/N]"; rnv$
  55. IF UCASE$(rnv$) = "Y" THEN GOTO s
  56.  
  57.  
  58. '4]  Modifiyng it and write to the same position:
  59. 'is as reading file:
  60. t:
  61. INPUT "Input record number (0-9) for rewriting"; recnr
  62. IF recnr < 0 OR recnr > 9 THEN PRINT "Invalid record": GOTO t
  63. 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!"
  64. INPUT "Insert new value for this position"; NewVal
  65. 'again: 1 record lenght is 1 byte. So first shift to area i n file contains previous record:
  66.  
  67. 'two ways how do it:
  68. SEEK #channel, recnr + 1 ' because record 0 start on byte 1 and not on byte 0. SEEK shift position in file
  69. PUT #channel, , NewVal
  70.  
  71. 'or in one step:      PUT #channel, recnr+1 , newval
  72.  
  73. 'now read and print all values from file to screen:
  74.  
  75. PRINT "In file "; File$; "are writed this values:"
  76. 'i use previous _UNSIGNED _BYTE type variable named as ValueNr
  77.  
  78. FOR read_it = 1 TO 10
  79.     GET #channel, read_it, ValueNr
  80.     PRINT ValueNr;
  81.  

To allow a NewVal record greater than 255 (on end this program) , this variable must be defined as another data type. This, of course, will change the length of the record in the file, so all subsequent values in the file will be invalid without any further modifications to the program.
« Last Edit: April 24, 2019, 10:43:44 am by Petr »

Offline Videolabguy

  • Newbie
  • Posts: 3
  • Squirrel?!
    • View Profile
Re: Binary file mode questions
« Reply #4 on: April 24, 2019, 09:18:44 pm »
Thank you kindly.