Author Topic: binary files  (Read 3960 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
binary files
« on: October 01, 2020, 11:30:26 am »
Hello

Here is a stupid question but i have a need to know. Can you use binary files if you are storing only string values.

Badger

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: binary files
« Reply #1 on: October 01, 2020, 11:52:40 am »
Yes specially if they are fixed length strings, otherwise you might have to mark the end of the strings.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: binary files
« Reply #2 on: October 01, 2020, 11:56:18 am »
Hello

the whole type is made of fixed length strings

Thanks

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: binary files
« Reply #3 on: October 01, 2020, 12:03:40 pm »
@badger

You did see the conversation about using Binary access as opposed to Random access for what I assume to be data-base like app, the thread you started here: https://www.qb64.org/forum/index.php?topic=3064.msg123418#msg123418

I don't see need for headers but eh! I haven't data based since 90's.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: binary files
« Reply #4 on: October 01, 2020, 12:10:32 pm »
hello

can you maybe explain these two lines as to what they do. i know what the get does but why the math there

GET #1, LOF(1) / LEN(a), a
PRINT LOF(1), LEN(a), LOF(1) / LEN(a), a

Phil

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: binary files
« Reply #5 on: October 01, 2020, 12:21:00 pm »
LOF(1) / LEN(a) = number of records if "a" is a record, that length of file LOF / length of record = number of records, so getting last record and calling it "a".

Update: This GET #1, is for Random access.
Code: QB64: [Select]
  1. DIM a AS STRING * 20
  2. OPEN "myfiletest.txt" FOR RANDOM AS #1 LEN = LEN(a)  ' <<<<<<<<<<<<<<<<<<<< for RANDOM
  3.     READ a
  4.     IF MID$(a, 1, 3) = "EOD" THEN EXIT DO
  5.     i = i + 1
  6.     PUT #1, i, a
  7.  
  8. OPEN "myfiletest.txt" FOR RANDOM AS #1 LEN = LEN(a)
  9. GET #1, LOF(1) / LEN(a), a
  10. PRINT LOF(1), LEN(a), LOF(1) / LEN(a), a
Binary access you get a byte # not a record number (usually) .

In Binary you access byte amounts according to size of a, instead of records. So "a" has to be record size to get 1 record.

« Last Edit: October 01, 2020, 12:39:54 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: binary files
« Reply #6 on: October 01, 2020, 12:30:48 pm »
hello

can you maybe explain these two lines as to what they do. i know what the get does but why the math there

GET #1, LOF(1) / LEN(a), a
PRINT LOF(1), LEN(a), LOF(1) / LEN(a), a

Phil

Expanding a bit on what Mark posted... Okay, that was taken from the example I provided and was in response to your question about getting the last record in a binary file. The LOF() is the keyword for finding the entire length of the binary file. a was dim in that entire code snippet, to represent a string of characters, set at a fixed length; therefore, LEN() is the keyword that provides us with that length. When you divide the total length of the binary file LOF(1) by the length of the fixed string records LEN(a) you get the location of the last record. For instance, if the entire binary file is 500 bytes, and the length of each fixed string is 100 bytes, then the last record can be found at 500 / 100, which would be record #5.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: binary files
« Reply #7 on: October 01, 2020, 12:34:10 pm »
Hello

Thanks for the explanation the last i used quick basic style programming was back in the 90s and i never have used binary files. I have gotten an idea from reading on here that it seems to be maybe faster and more efficient so i wanted to try to use them. Yes it is a data base app with more than one file. The last time i wrote a database app was using qb4.5 and btrieve. if you are not familiar with btrieve. I used btrieve for dos 5.10a google it or goto pervasive.com and read up on the newest version. It is to expensive to buy now it is 5000.00 cant afford that so i have to come up with a way to access all the data and tie it together.

by the way I absolutely hate sql LOL

Badger

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: binary files
« Reply #8 on: October 01, 2020, 12:36:41 pm »
Pete

Thanks for the expanded explanation of that. I thought that was what it was but now understand.

Badger