I'm pretty sure read is impure and modifies $f's read pointer.
Not sure what you are trying to say here.
This is what Tcl's read command does:
https://wiki.tcl.tk/1182
If that is considered "impure" then so be it, it's just the way it's supposed to work.
you should be able to just read while [eof $f] == 0. if not, you should be able to use "tell" and math in order to get the next argument for seek.
I'm not sure why you would suggest something like this? Using seek and tell and math doesn't help the OP read this well formatted data. The OPs problem seemed to stem from an issue with the LF being in the character channel stream. This would also require a different script depending on the file format, unix/dos, which would make the loop even more complicated.
Just read the line like I suggested in #5 and then parse the resulting string into the individual 2 character hex bytes. No reason to make anything a "number" (e.g.
everything is a string) and use math to extract the bytes. Hell the OP could just read the line and shift the characters one by one building each byte...(but my suggestion is way easier)
Of course my original suggestion using regex is probably somewhat slower than some other implementations (Regex being a somewhat slow process and Tcl having had a poor implementation of it), but considering they are unlikely concerned about ms of time difference in the script...why care?.
- - - Updated - - -
Here is code that reads the following file in:
Code:
12345678
9abcdef0
AAAA5555
F0F0F0F0
and spits out this:
Code:
12
34
56
78
9a
bc
de
f0
AA
AA
55
55
F0
F0
F0
F0
F0
F0
F0
F0
Note this code does not write the data to another file, that is left to the reader.
Code:
proc byte_me {file {eolchar "\n"}} {
set f [open $file r]
while {![eof $f]} {
gets $f line
regexp {(..)(..)(..)(..)} $line matched byte1 byte2 byte3 byte4
puts "$byte1\n$byte2\n$byte3\n$byte4"
}
close $f
}
Took all of 3 mins to write and test, probably against my better judgement I should have just supplied the entire code in post #5.
On a Tcl prompt %
source byte_me.tcl
byte_me <filename_to_byte>
- - - Updated - - -
Oops, I just noticed you wanted to deal with each byte separately...
Code:
proc byte_me {file {eolchar "\n"}} {
set f [open $file r]
while {![eof $f]} {
gets $f line
regexp {(..)(..)(..)(..)} $line matched byte1 byte2 byte3 byte4
foreach byte [list $byte1 $byte2 $byte3 $byte4] {
puts "$byte, i.e. the stuff you want to do to each byte separately"
}
}
close $f
}