How to? read while ignoring whitespace

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
I have a file with contents similar to the following:
Code:
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?
-Scott
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
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:
Code:
:readmore
set line=%@inc[line]
set l=%@fileread[%qf]
set cw=0
iff "%l"=="**EOF**" then
    set qf=%@fileclose[%qf]
    quit
endiff
return
And I then created helper routines for reading 16 bit words, 32 bit words, structures, and strings:
Code:
: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]
enddo
echo.
return
:ReadWord   
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]
return
:String
gosub ReadWord
echos %wrd ``
gosub ReadBytes %@convert[16,10,%wrd]
if not '%str'=='' echo %str
return
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
Thread starter Title Forum Replies Date
K "copy /z /w" command not deleting read-only files in destination Support 10
T unqlite binary read test is inconsistent Support 2
A TCC failing to read recursive symlinks Support 25
Alpengreis [Forum] Mark forumS (all!) read Support 2
D Custom ini-file is not read, by tcmd.exe cli Support 6
T read snmp values Support 4
WinLanEm Read Cyrillic text from a file Support 12
C How to? Read Win7's Computer Libraries Support 19
T [TCC 18] Can't read nor write history Support 1
Charles Dye Read-only environment variables wiped by SETLOCAL / ENDLOCAL Support 5
MickeyF Trying to read XML Support 3
R attrib -C on a read only file? Support 1
S WAD "Mark Forums Read" fails Support 4
vefatica Can DIRHISTORY read from clip:? Support 2
vefatica Mark a forum or thread as "read"? Support 5
M How to? How do I read a Unicode file through standard-input? Support 4
daniel347x How to get Take Command to read in all current system environment variables that CMD.EXE sees? Support 13
C Attaching PowerShell changes its colors and makes it hard to read Support 15
T Is there a way to maintain the position in the environment list, while debugging? Support 10
N Command execution slows down in TCC Prompt after a while Support 6
old coot Proper use of ON ERRORMSG inside a DO WHILE loop? Support 3
Alpengreis Fixed 0xc0000005 while try to create a button (Schaltfläche) Support 4
I Change value of a variable while debugging Support 3
fromano Fixed /E not working in TCMD 17.0.40 while doing one way synch Support 1
C How to? run a batch file in administrator mode while debugging in take command software Support 1
Mordachai Fixed Closing TCMD while IDE + Help Open = Crash Support 7
vefatica Browser doesn't start while IDE is running Support 12
F unexpected %1 while starting ide Support 6
M (Somewhat unbelievable) Do While Behavior/Question Support 1
S Ctrl-C handling while piping Support 0
B While This Be Fixed? Support 2
A WAD Take Command ignoring my application-extension associations Support 19

Similar threads