sandyrepope
16-02-2007, 02:57
I've been trying to write a routine to get information from a binary file and so far can't get it to work.
This is what I've been trying:
uses "CONSOLE"
uses "FILE"
dim tempval as long
dim i as integer
dim some as byte
dim gFile as string
gFile = "test"
tempval=file_open( gFile ,"BINARY")
for i=1 to 7
some = file_get(tempval, 1)
console_write(mkbyt$(some))
console_writeline("")
next
file_close(tempval)
I think it's obvious that I don't know what I'm doing. Can someone clue me in on what I'm doing wrong? It doesn't get anything from the file.
Michael Hartlef
16-02-2007, 08:51
Ok, I'm just guessing here. The filename has no path and extention. Are you sure it found it? Check it's return value.
For the rest I have to study thinBasic a little more. Sorry.
ErosOlmi
16-02-2007, 09:05
Hi Sandy,
I'm not in office now, so I will check later but there should be an error in help file about FILE_GET function. It should return a string on n bytes and not a number. I will fix help file for next release.
See this example.
Ciao
Eros
uses "CONSOLE"
uses "FILE"
dim fHandle as long
dim i as integer
dim some as string
dim gFile as string
'---Set file name to open
gFile = app_path & "thinbasic.exe"
'---Open file
fHandle = file_open(gFile, "BINARY")
'---Get first 20 bytes ...
for i=1 to 20
'---...each byte. FILE_GET returns a string composed by the number of bytes
'--- has been specified to be taken from the open file handle
some = file_get(fHandle, 1)
'---Write out ascii code and char only if printable
console_writeline format$(asc(some), "000") & " " & iif$(asc(some) > 32, Some, "")
next
'---Close file
file_close(fHandle)
'---Wait for user to press a key
console_waitkey
ErosOlmi
16-02-2007, 13:50
Sandy,
I had some time to implement your example and add comments.
Here it is the code and also as attached file below
Hope it will help you.
Ciao
Eros
'------------------------------------------------------------------
' Print out any file on screen in HEX and text format
'
' This script will read a file from disk and will print
' on screen its hex decimal representation. sBufferLen bytes
' of data will be loaded for each loop
'------------------------------------------------------------------
'---Declare modules to be used in this script
uses "CONSOLE"
uses "FILE"
'---Define needed variables
dim fHandle as long '---File handle
dim Counter as long '---Used to count bytes
dim Looper as long '---Used to loop into string buffer bytes
dim sBuffer as string '---Will store read data from file
dim sChar as string '---Used for single byte tokenizing
dim sLine as string '---Used for line our composing
dim gFile as string '---File name to be loaded
dim sBufferLen as long value 16 '---Number of bytes to be read for every line loop
'---The following data is used for string replace in order to output
'---binary strings on screen.
dim ReplaceBinIn as string value chr$(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31)
dim ReplaceBinOut as string value "................................"
'---Start timers
dim T0, T1 as double = timer
'---Set file name to open
gFile = app_path & "thinbasic.exe"
'---Open file
fHandle = file_open(gFile, "BINARY")
'---While not at end of file ...
while FILE_EOF(fHandle) = %FALSE
'---Write byte counter in multiple of sBufferLen
console_write format$(Counter, "00000000") & " "
'---Load sBufferLen bytes in sBuffer
sBuffer = file_get(fHandle, sBufferLen)
'---Increment Counter by sBufferLen
Counter += sBufferLen
'---Now loops for all sBufferLen bytes in order to print HEX values
sLine = ""
for Looper = 1 to sBufferLen
'---Get single byte
sChar = mid$(sBuffer, Looper, 1)
'---Write out ascii code in hex format
sLine += hex$(asc(sChar), 2) & " "
next
console_write sLine
'---Now we print out sBufferLen buffer but before to be able to print
'---we have to change ascii chars from 0 to 31 with something printable
sBuffer = replace$(sBuffer, any ReplaceBinIn, ReplaceBinOut)
'---Now we can print it and go next line
console_writeline sBuffer
wend
'---Close file
file_close(fHandle)
'---Set finish time
T1 = timer
'---Output some info
console_writeline string$(79,"-")
console_writeline "File analized: " & gFile
console_writeline "File size: " & file_size(gFile) & " bytes"
console_writeline "Operation performed in " & format$(T1 - T0, "#0.0000") & " seconds."
'---Wait for user to press a key
console_waitkey
Wow beautiful looking code Eros and cool example, thanks!