By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to? read while ignoring whitespace


Scott Mintz
I have a file with contents similar to the following:
8e 00 01 00 00 00 00 00 00 00 00 00 00  
   00 7f 00 01 00 69 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 7f 00
   51 75 69 63 6b 2d 44 69 72 74 79 20 54 65 73 74 20 66 6f 72 20 4d 6f 74 69
I want to read the individual hex bytes in a continuous fashion and ignore the end of line characters and leading whitespace. Is there an easy way to do that?
To be more specific, the data forms records. The first column is 12 bytes. The second is 2, and so on. I want to just read the file in that way - i.e. read 12, read 2, and so on. So using the above as an example, I want to read "8e 00 01 00 00 00 00 00 00 00 00 00" with the 12 byte read and "00 00" on the 2 byte read, and "7f 00" and the next read, etc. That second read spans EOL.
Any easy ideas?
Won't alternating %@fileread[handle,12] and %@fileread[handle,2] do the job? Or possibly %@ascii[%@fileread[handle,12]] etc.? Or you may consider using %@saferead[handle,12]?
If your data contains null bytes, I suspect you'll have to use binary buffers to handle it.
Steve, @fileread with a number reads literally, that many bytes. It doesn't do what I need. I ended up coding a solution that worked out pretty well in the end.

I created a subroutine named ReadMore:
set line=%@inc[line]
set l=%@fileread[%qf]
set cw=0
iff "%l"=="**EOF**" then
    set qf=%@fileclose[%qf]
And I then created helper routines for reading 16 bit words, 32 bit words, structures, and strings:
:ReadBytes [howmany]
set str=
do i=1 to %howmany
    if %cw == %@words[%l] gosub ReadMore
    echos %@word[%cw,%l] ``
    set str=%[str]%@char[0x%@word[%cw,%l]]``
    set cw=%@inc[cw]
if %cw == %@words[%l] gosub readmore
set wrd=%@word[%cw,%l]
set cw=%@inc[cw]
if %cw == %@words[%l] gosub readmore
set wrd=%@word[%cw,%l]%wrd
set cw=%@inc[cw]
gosub ReadWord
echos %wrd ``
gosub ReadBytes %@convert[16,10,%wrd]
if not '%str'=='' echo %str
Then I just had a loop that iterated through the structure gosub-ing each data type.
The big issue I was trying to track down was where in this 100K file was the data corrupted? After running this routine I was able to track and find exactly where, which gave me a clue as to why it was happening.
I was very excited. (But I'm easily impressed).

Similar threads