Qbasicnews.com

Full Version: Trouble with Binary File
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have written this file, but it will not work properly:

CLS
INPUT "CHURCH ";CHURCH$ ' I use FIRST as input
INPUT "SPEAKER ";SPEAKER$ ' I use ASSEMBLY as input

OPEN "CNAME.DTA" FOR BINARY AS #1
PUT 1,1, CHURCH$
PUT 1,2, SPEAKER$ ' The 1 is for file #1 and the 2 is for position # 2
CLOSE #1

CHURCH$ = "" ' clearing the inputs to see what the file will do.
SPEAKER$ = ""

OPEN "CNAME.DTA" FOR BINARY AS #1
GET 1,1, CHURCH$
GET 1,2, SPEAKER$
CLOSE #1

PRINT "CHURCH = ";CHURCH$
PRINT "SPEAKER = ";SPEAKER$
END

'THE COMPUTER OUTPUT IS

CHURCH = FASSEMBLY
SPEAKER = ASSEMBLY

Why Does Church come out as FASSEMBLY?
What Am I doing wrong?

Please Help
You are actually referring to the byte position in your file, not the record position.

To let QB jump to the next byte position, do:

PUT #1, , church$
PUT #1, , speaker$
Thanks for your help. I tried this and it solved my problem.

Again, thanks for your help Agamemnus
Actually using binary files is simple once you understand how they work.
When you are writing to a binary file you must know exactly how many bytes you are writing. Since you will need this while reading.

Lets take your prog as an example. When u are PUTing at 1 it means you are writing at byte position 1. The no. of bytes you are writing is equal to the length of the string i.e. 5(FIRST). Now your file looks like this:
Code:
BYTE   1  2  3  4  5...
CHAR   F  I   R  S  T

The next line writes to the 2nd byte. So your file now looks like this:
Code:
BYTE   1  2  3  4  5  6  7  9  10
CHAR   F  A  S  S  E  M  B  L  Y

If you want to use binary files, I recommend using structures i.e. TYPES rather than just variables.