So here is the problem

Anything QL Software or Programming Related.
Post Reply
User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

So here is the problem

Post by Mr_Navigator »

As part of a program I have got to the point where I am not sure what I am doing wrong and probably don't know enough about how files are stored in particular BIN files.

I am using the GET/PUT command from the TK2 and don't really want to use any other extensions other than that found in TK2.

The documentation for the big RWAP book shows thus

S y n t a x : GET [ # c h a n n e l \ f i l e _ p o s i t i o n , ] [ v a r 1 * [ , v a r i ] * . . . ]

with integer% (range - 32768 to 32767) stored as 4 bytes

and I am using this in the following procedure


870 DEFine PROCedure OpenBinFile
880 f$ = "FLP1_aliens_bin"
890 OPEN_IN#3, f$
900 OPEN_NEW#4, "flp1_testdata"
910 PRINT FLEN
920 PRINT FPOS
930 FOR f%=1 TO FLEN-1 (this isnt working correctly yet, probably for the same reasons as stated below)
950 GET #3\f%, a%
960 PRINT FPOS,a%
970 PUT #4,a%
980 END FOR f%
990 CLOSE#3
995 CLOSE#4
996 end def

When comparing the two files they differ significantly and I suspect using f% and a% is wrong but I am at a loss what to try next, not knowing how bin files are stored.

I am trying to recreate a duplicate file using this method, but want to add other bits to it.
the original BIN file must remain intact at the end of the whole process as there are a few steps still to go, however until I can get this bit working I cannot move to the next stage.

Can anyone help please?


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
User avatar
JonS
Bent Pin Expansion Port
Posts: 76
Joined: Fri Nov 11, 2011 3:54 pm
Location: Cumbria

Re: So here is the problem

Post by JonS »

I suggest you should be reading a byte at a time using BGET/BPUT rather than GET/PUT. Also, no need to set file position each time if you are just reading from start to finish.

Regards


User avatar
dilwyn
Mr QL
Posts: 2753
Joined: Wed Dec 01, 2010 10:39 pm

Re: So here is the problem

Post by dilwyn »

Mr_Navigator wrote:
870 DEFine PROCedure OpenBinFile
880 f$ = "FLP1_aliens_bin"
890 OPEN_IN#3, f$
900 OPEN_NEW#4, "flp1_testdata"
910 PRINT FLEN
920 PRINT FPOS
930 FOR f%=1 TO FLEN-1 (this isnt working correctly yet, probably for the same reasons as stated below)
950 GET #3\f%, a%
960 PRINT FPOS,a%
970 PUT #4,a%
980 END FOR f%
990 CLOSE#3
995 CLOSE#4
996 end def

When comparing the two files they differ significantly and I suspect using f% and a% is wrong but I am at a loss what to try next, not knowing how bin files are stored.

I am trying to recreate a duplicate file using this method, but want to add other bits to it.
the original BIN file must remain intact at the end of the whole process as there are a few steps still to go, however until I can get this bit working I cannot move to the next stage.

Can anyone help please?
Two or three possible issues here:
1. Use BGET and BPUT in place of GET and PUT if the FOR loops work in steps of one. e.g.
FOR f%=0 to FLEN(#3)-1
BGET #3\f%,a%
BPUT #4,a%
END FOR f%
CLOSE #3 : CLOSE #4 (or in SBASIC just CLOSE to close all open channels)

Another way to write this might be to check for end of file as you go along, probably not as good style as how you write it using a FOR though.

REPeat
IF EOF(#3):EXIT
PRINT #4,INKEY$(#3);
END REPeat
2. FPOS and FLEN will work on some systems without channel numbers, but you may find it clearer to give an explicit channel number if you have more than one open.
3. On some systems, FLEN does not give the true file length sometimes when a channel is still open to a write file, e.g. OPEN_NEW #3,ram1_test:BPUT #3,10:PRINT FLEN(#3) can give 0 in some cases instead of the 1 you might expect, probably to do with buffer flushing, so try FLUSH #3 and you get the result you expect.
4. FOR f% = ... - an integer variable in a FOR loop can only be used in SBASIC and IIRC Minerva (not in standard QDOS).


EmmBee
Trump Card
Posts: 240
Joined: Fri Jan 13, 2012 5:29 pm
Location: Kent

Re: So here is the problem

Post by EmmBee »

Mr_Navigator wrote:The documentation for the big RWAP book shows thus

S y n t a x : GET [ # c h a n n e l \ f i l e _ p o s i t i o n , ] [ v a r 1 * [ , v a r i ] * . . . ]

with integer% (range - 32768 to 32767) stored as 4 bytes
The QPC Keyword Reference Guide and the TK2 manual also describe the GET/PUT commands. However, they both have an integer stored as 2 bytes and not the 4 bytes quoted in the RWAP book!

Assuming that the figure of 2 bytes to be correct, we could take as an example a BIN file containing five such integers. The file length would be 5 x 2 = 10 bytes. Unlike strings, files start at position 0, so the FOR loop will need to access the five positions 0, 2, 4, 6 and 8. The first position will be 0, and the final position will be 8, which is 2 less than the file length or FLEN-2. So, my reckoning is that
line 930 in your program should be ...

Code: Select all

                     930 FOR f% = 0 TO FLEN-2 STEP 2   
You could try the above if you haven’t done so already. Of course, you are probably aware that there is a quick way of doing all this by using COPY to duplicate the file ...
COPY “FLP1_aliens_bin” TO “flp1_testdata”.
But even if you do that you will still need to master how to use the PUT command when you come to the next stage of adding other bits to your testdata file. What you might find helpful is to draw on a piece of paper a diagram and label the bytes accordingly. This could help to verify what the values of the positions should be.

EmmBee


User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

Re: So here is the problem

Post by Mr_Navigator »

!Update!

Have made the suggested changes regarding BGET and BPUT and this appears to work.

I have looked at the original file and the copied file in Microsoft Wordpad and the patterns appear the same, so now I want to look at the HEX code to satisfy my paranoia before I continue.

I am reminded in the early days of Microsoft DOS of a file manager type program called, of all things, Q-DOS II (Gazelle Systems). It could copy delete etc. etc. using a DOS window cursor controlled interface and was very fast.

One brilliant feature was the ability just to highlight a file and look at it in HEX form.

I used it extensively to look/hack at saved games in Commander Keen and Wolfenstein and early Command & Conquer saved games looking for lives and money values and #FF the values to get maximum yey :)
Untitled-2.png
So what I am now looking for is a QL HEX editor. I have looked at Dilwyn's site but can only find Text editors. Can anyone point me in the right direction?


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
swensont
Forum Moderator
Posts: 252
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: So here is the problem

Post by swensont »

Mr. Navigator,

Get me via PM (swenson_t@sbcglobal.net). A while back I did a ascii dump program, and it was fairly easy to convert it to do hex dumps. I can send you the executable that should work for your needs.

Tim Swenson


Post Reply