Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CONFUSING ARTICLE: Random Access Files - By Cruzan Wallis
#6
Sure, and it's terribly simple:

Code:
GET #file%, pos&, <data>
PUT #file%, pos&, <data>

Those, respectively, get <data> from or put <data> into the file. The number of bytes depends on the length of the data type the <data> variable is. For example,

GET #1, 10, a%

Will read two bytes from position 10 and will store them in a%

PUT #1, 200, great$

Will write LEN(great$) bytes from position 200 taken from the great$ string.

You can also use them to access a file sequentially. There is a file pointer just like in INPUT and OUTPUT sequential modes and you can tell QB to read or write from that position just not telling PUT or GET a position, and that pointer gets automaticly incremented by the size of the data read or written. For example, the following code will read a binary file completely byte to byte:

Code:
byte$ = " "   ' Just to make byte$ measure 1 byte.

f%=FREEFILE
OPEN "FILE.BIN" FOR BINARY AS #f%

WHILE NOT EOF(f%)
   GET #f%, , byte$
   PRINT "Byte read: "; ASC(byte$)
WEND
CLOSE #f%

You can also write or read a full record, QB will calculate how many bytes depending on the size of the custom data type, and will increase the file pointer correctly as well:

Code:
TYPE RecordType
   id AS INTEGER
   name AS STRING * 20
   age AS INTEGER
   phone AS STRING * 10
END TYPE

DIM myRecord AS RecordType

f%=FREEFILE
OPEN "FILE.DAT" FOR BINARY AS #f%

PUT #f%, , myRecord

CLOSE #f%

or the same way around:

Code:
GET #f%, , myRecord

As you see, this is far way more flexible than the old RANDOM mode, not limiting the size of the data/write read or where to read/write that data.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Messages In This Thread
CONFUSING ARTICLE: Random Access Files - By Cruzan Wallis - by na_th_an - 05-12-2004, 07:34 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)