aeolus
aeolus

Reputation: 159

TCL: Write binary/logical data to file

I am trying to write a list of 1's or 0's to file in tcl. I expect the most efficient way would be to write this in binary format to use the least amount of bits possible, especially because I anticipate dealing with many megabytes of data. I am following examples from:

https://groups.google.com/forum/#!msg/comp.lang.tcl/HrC-VlfRL_E/PAQdLRTyrMEJ http://wiki.tcl.tk/1180

but when I go to read my binary data in as per the examples, I literally get back the work "binary". What is going on?

Upvotes: 0

Views: 3622

Answers (1)

aeolus
aeolus

Reputation: 159

Examples above use the syntax

[format binary c1 0 1 1]

but should transpose 'format' and 'binary'

[binary format c1 0 1 1]

as per https://www.tcl.tk/man/tcl8.5/TclCmd/binary.htm#M4

Example Script that gives the desired results (tcl 8.5 maybe other versions):

set fp [open text.bin w]
set outBinData [binary format ccc 1 0 1 ]
puts "Format done: $outBinData"
puts -nonewline $fp $outBinData
close $fp
set fp [open text.bin r]
set inBinData [read $fp]
close $fp
binary scan $inBinData ccc val1 val2 val3
puts "Scan done: $val1 $val2 $val3"

Upvotes: 2

Related Questions