July 23, 2024, 09:38:39 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Save As Binary

Started by aurelCB, August 05, 2009, 03:35:39 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

Hi all...
I made one small example of how save file as binary.
I just modify little bit one piece of code from Asteroids example.

' Save As Binary file...test
DEF win:window
def hs:BFILE
def topten[4]:char
def score:int

Window win,0,0,400,300,@minbox,0,"Save As Binary",main

'create a blank one if it doesn't exist
IF(openfile(hs,GETSTARTPATH + "Aurel.test1","R") = 0)
for x = 1 to 4
GET hs,x,topten[x-1]
next x
closefile hs
ELSE
if(openfile(hs,GETSTARTPATH + "Aurel.test1","W") = 0)
topten[0] = &H50
topten[1] = &H45
topten[2] = &H0
topten[3] = &H0
for x = 1 to 4
PUT hs,x,topten[x-1]
next x
closefile hs
'IF topten[3] = &H0 then Messagebox 0,"File updated","OK!",64
endif
ENDIF
IF topten[3] = &H0 then Messagebox 0,"File updated","OK!",64

waituntil win=0
end

Sub main
select @class
case @idclosewindow
closewindow win
endselect
return


Then i search for new file in Creative samples folder and found them with name Aurel.test1.
So this is OK file is saved, then i open this file with Notepad just for see what is in there.
And found something weird it says  PE E.
Hmm what is That?

GWS

Hi Aurel,

Binary files write incomprehensible stuff to the file.  The only way to recover data from a binary file is to read it back the same way as you wrote it.

In this case using the corresponding GET statements.

&H50, &H45  are integer 80 and 69 - and these are characters P and E in the ASCII table.

Nothing wrong, it's just that the binary format screws up your data values ..  :)

all the best,

Graham
Tomorrow may be too late ..

aurelCB

Hi mr.Graham... :)

Stupid me... ::)
I forget to tell from where i use this values (bytes).
This values is from small VB written PE header and is obvius present PE exe header
written in binary.I just mess somethings like usual ::) ::)
But exactly work as espected .
This code is start when linker made PE executable and i supose that is part of any
windows PE exe file( i think...).
I will make more tests becose i find the rest of code.
So not bad , Cbasic program can make exe with code .... cool isn't  ;D ;D ;D

all best...
aurel