Author Topic: [video] File I/O in QB64 (part 2/2)  (Read 3551 times)

0 Members and 1 Guest are viewing this topic.

FellippeHeitor

  • Guest
[video] File I/O in QB64 (part 2/2)
« on: June 27, 2021, 10:27:33 pm »
This time we go a bit further and learn to open files for RANDOM or BINARY access mode.



For part 1: https://www.qb64.org/forum/index.php?topic=3986.0

Here's the list of "chapters"/segments:
00:00 Intro
00:25 Part 1 recap
01:49 Defining a record structure (TYPE)
04:00 Opening file for RANDOM access
05:47 Using PUT # to save data to a file on disk
06:25 Development build - help us beta test the next version
07:44 Back to PUT # data to the file
08:43 Inspecting the data we output to disk
10:33 Fetching data back from the file on disk
13:35 Detecting how many records are stored in the file
14:57 Using SEEK to move about in the RANDOM file
16:15 Adding more data to the file
18:24 Showcasing how QB64 helps you control input
19:45 Reading data back in sequential order
21:12 Reading data back in RANDOM order
25:28 BINARY mode differences
29:01 Emulating a binary format - Using PUT #
31:30 Reading data back from a binary file with GET #
33:01 Writing to any position in a BINARY file
36:34 Outro and thank you!

Marked as best answer by on Yesterday at 06:07:17 pm

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: [video] File I/O in QB64 (part 2/2)
« Reply #1 on: June 28, 2021, 09:14:18 am »
  • Undo Best Answer
  • Well done.  If I didn't know these commands already these tutorials would be all I need to understand them.

    - Dav

    FellippeHeitor

    • Guest
    Re: [video] File I/O in QB64 (part 2/2)
    « Reply #2 on: June 28, 2021, 09:29:12 am »
    Thanks, Dav!

    Offline Dimster

    • Forum Resident
    • Posts: 500
      • View Profile
    Re: [video] File I/O in QB64 (part 2/2)
    « Reply #3 on: June 28, 2021, 10:02:49 am »
    Thanks Fell, that was very helpful. If I understand it, an existing Random data file would have fixed length for each record, says 160. To access those records using Binary would be Seek 1, Seek 161, seek 322 , seek 483 etc. If you have an existing Sequential file how would you access those records using Binary? Is there any speed advantage in either the read or the write using Binary?
    Thanks again

    Offline SMcNeill

    • QB64 Developer
    • Forum Resident
    • Posts: 3972
      • View Profile
      • Steve’s QB64 Archive Forum
    Re: [video] File I/O in QB64 (part 2/2)
    « Reply #4 on: June 28, 2021, 11:21:53 am »
    Thanks Fell, that was very helpful. If I understand it, an existing Random data file would have fixed length for each record, says 160. To access those records using Binary would be Seek 1, Seek 161, seek 322 , seek 483 etc. If you have an existing Sequential file how would you access those records using Binary? Is there any speed advantage in either the read or the write using Binary?
    Thanks again

    Sequential files have *something* which separates the records — usually a comma, or End of Line character.

    The easiest way to read them, in BINARY mode, is to just read the whole file at once and then parse it for those characters.

    OPEN file$ FOR BINARY AS #1
    FileData$ = SPACE$(LOF(1))
    GET #1, 1, FileData$
    CLOSE #1

    Now that the whole file is in FileData$, just parse it with INSTR(FileData$, “,”) and INSTR(FileData$, CHR$(10)) to get records one at a time.

    (Usually, if I need to access the data multiple times, I’ll just load it once as above and then save it in an array for future reference and to prevent unnecessary disk reads/writes.)
    https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

    Offline Dimster

    • Forum Resident
    • Posts: 500
      • View Profile
    Re: [video] File I/O in QB64 (part 2/2)
    « Reply #5 on: June 28, 2021, 12:02:11 pm »
    Perfect Steve - just perfect. Thanks